Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
socialsecurity
Aug 30, 2003

Hey working on an Android app with Zendesk SDK integration I need to do a simple HTTP POST request with basic authentication so I can get a user token back from a ASP script I'm hosting on IIS but I cannot for the life of me get it going. Every single example I see is somehow outdated even when I try to import the libraries they are using can anyone point me towards a good resource to get this done?

Adbot
ADBOT LOVES YOU

Elias_Maluco
Aug 23, 2007
I need to sleep

socialsecurity posted:

Hey working on an Android app with Zendesk SDK integration I need to do a simple HTTP POST request with basic authentication so I can get a user token back from a ASP script I'm hosting on IIS but I cannot for the life of me get it going. Every single example I see is somehow outdated even when I try to import the libraries they are using can anyone point me towards a good resource to get this done?

In my last project Ive used OkHttp, it worked pretty well, and is very easy to use:

http://square.github.io/okhttp/

There are POST examples there.

The only problem I had was when I needed to upload a file, for that the documentation is kinda wrong (I did got it working later).

EDIT: that example is actually kinda weird, here is what worked for me (receiving a JSON as response):

code:
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add('field', 'value');

RequestBody requestBody = builder.build();

Request request = new Request.Builder().url('http://your-url.com').post(requestBody).build();

client = new OkHttpClient();

try
{
	Response response = client.newCall(request).execute();

	ObjectMapper mapper = new ObjectMapper();

	String body = response.body().string();

	PojoClass response = (PojoClass)mapper.readValue(body, PojoClass.class);	
}//try client execute
catch(IOException e)
{
	...
}//cacha e
"PojoClass" being any simple simple class with getters/setters matching the JSON response object.

Elias_Maluco fucked around with this message at 21:31 on Dec 14, 2015

speng31b
May 8, 2010

socialsecurity posted:

Hey working on an Android app with Zendesk SDK integration I need to do a simple HTTP POST request with basic authentication so I can get a user token back from a ASP script I'm hosting on IIS but I cannot for the life of me get it going. Every single example I see is somehow outdated even when I try to import the libraries they are using can anyone point me towards a good resource to get this done?

You're finding old examples recommending the Apache http client, right? Use okhttp as your Android http client as the above poster recommends. Apache examples are littered around the internet because its api is simple and it's built in, but don't ever use it, it's been deprecated for so long that it's finally, thankfully gone in the latest sdks.

KICK BAMA KICK
Mar 2, 2009



New at the IntelliJ IDEA GUI designer. Is there a good way to get the selected panel in the above image containing the three buttons to be actually centered in the window, not pushed a little bit left by the Finish button? I tried a few arrangements with the Finish button inside that panel and a horizontal spacer between it but then the Finish button wouldn't push all the way to the right edge.

And also to enforce a minimum size on the whole window so it can't be shrunk smaller than needed to display those controls need -- do I have to manually figure out the number of pixels or can I do it with policies and maybe spacers?

KICK BAMA KICK fucked around with this message at 00:40 on Dec 15, 2015

Volguus
Mar 3, 2009
While I dabbled a bit with Android, never had a need for making a POST request to a service. Therefore, I have a question:

Doesn't the Android SDK have the java.net classes? Specifically URL, with URLConnection and such? Because, for simple stuff, you cannot really beat the builtin classes. For more complex things, yes use other libraries (Apache's or this OkHttp), but simple stuff ... why bother?

Volguus
Mar 3, 2009

KICK BAMA KICK posted:



New at the IntelliJ IDEA GUI designer. Is there a good way to get the selected panel in the above image containing the three buttons to be actually centered in the window, not pushed a little bit left by the Finish button? I tried a few arrangements with the Finish button inside that panel and a horizontal spacer between it but then the Finish button wouldn't push all the way to the right edge.

And also to enforce a minimum size on the whole window so it can't be shrunk smaller than needed to display those controls need -- do I have to manually figure out the number of pixels or can I do it with policies and maybe spacers?

Don't use designers for Swing. Really. They are more of a bother than help. For what you want, you can achieve that very easily with GridBagLayourManager. Here's the documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Essentially, put the finish button in the panel, have a panel (spacer) in between with a weight of 1, and adjust the insets to control the margins. But anyway, I don't think this is a very intuitive design, would be better with the finish button beside the other buttons, but whatever your homework said.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

KICK BAMA KICK posted:



New at the IntelliJ IDEA GUI designer. Is there a good way to get the selected panel in the above image containing the three buttons to be actually centered in the window, not pushed a little bit left by the Finish button? I tried a few arrangements with the Finish button inside that panel and a horizontal spacer between it but then the Finish button wouldn't push all the way to the right edge.

And also to enforce a minimum size on the whole window so it can't be shrunk smaller than needed to display those controls need -- do I have to manually figure out the number of pixels or can I do it with policies and maybe spacers?

See on the left, where it says Layout Manager - GridLayoutManager ? That's what's controlling the behavior of moving things around. Try different layouts, possibly even absolute value layout, and then you can place things in different places that your current layout may just not allow.

E: See what Volguus said

socialsecurity
Aug 30, 2003

Elias_Maluco posted:

In my last project Ive used OkHttp, it worked pretty well, and is very easy to use:

http://square.github.io/okhttp/

There are POST examples there.

The only problem I had was when I needed to upload a file, for that the documentation is kinda wrong (I did got it working later).

EDIT: that example is actually kinda weird, here is what worked for me (receiving a JSON as response):

code:
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add('field', 'value');

RequestBody requestBody = builder.build();

Request request = new Request.Builder().url('http://your-url.com').post(requestBody).build();

client = new OkHttpClient();

try
{
	Response response = client.newCall(request).execute();

	ObjectMapper mapper = new ObjectMapper();

	String body = response.body().string();

	PojoClass response = (PojoClass)mapper.readValue(body, PojoClass.class);	
}//try client execute
catch(IOException e)
{
	...
}//cacha e
"PojoClass" being any simple simple class with getters/setters matching the JSON response object.

Awesome thank you this was right along the idea of what I was trying to accomplish I think I need some to make some slight modifications for basic auth and I will be finally making some progress on this.

KICK BAMA KICK
Mar 2, 2009

Thanks; I actually had started writing it out manually and then tried the designer just to see what it was like. I did like having an actual GUI to quickly tweak settings rather than sifting through the volume of code to find where the constraints were established for a particular component or whatever. I'll try going back to that and just organizing my code better this time. Not homework, just a toy I was screwing around with to learn Swing. The design made sense to me to separate the three buttons that relate to the displayed image from the Finish one that ends the current mode of operation (and to put Back and Next symmetrically offset from the center), but I don't actually know anything about UX.

speng31b
May 8, 2010

Volguus posted:

While I dabbled a bit with Android, never had a need for making a POST request to a service. Therefore, I have a question:

Doesn't the Android SDK have the java.net classes? Specifically URL, with URLConnection and such? Because, for simple stuff, you cannot really beat the builtin classes. For more complex things, yes use other libraries (Apache's or this OkHttp), but simple stuff ... why bother?

Depends on how simple it is. Most Android apps beyond the most hello worldy piece of junk will benefit from a proper networking library that also handles threading for you to some extent, delivers responses cleanly, and plays well with rxjava. But yeah, occasionally as a one off I'll find myself spinning up a raw httpurlconnection.

socialsecurity
Aug 30, 2003

socialsecurity posted:

Awesome thank you this was right along the idea of what I was trying to accomplish I think I need some to make some slight modifications for basic auth and I will be finally making some progress on this.

So trying this out running into a weird issue

I compiled the Okio and OkHTTP libraries but it still doesn't recognize like half the commands like the .request, not sure maybe I need to import something else or what I'm missing here.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

socialsecurity posted:

So trying this out running into a weird issue

I compiled the Okio and OkHTTP libraries but it still doesn't recognize like half the commands like the .request, not sure maybe I need to import something else or what I'm missing here.

Paste the code, not the screenshot. Also, check your imports and make sure you're using the right thing.

socialsecurity
Aug 30, 2003

Volmarias posted:

Paste the code, not the screenshot. Also, check your imports and make sure you're using the right thing.
Compile in my Grade
compile 'com.squareup.okhttp:okhttp:2.7.0'
compile 'com.squareup.okio:okio:1.6.0'

Importing from the mainactivity,
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Credentials;
import com.squareup.okhttp.FormEncodingBuilder;

Code
code:
final OkHttpClient client = new OkHttpClient();
//    public void run() throws Exception {
        client.setAuthenticator(new Authenticator() {
                                    @Override
                                    public Request authenticate(Proxy proxy, Response response) {
                                        System.out.println("Authenticating for response: " + response);
                                        System.out.println("Challenges: " + response.challenges());
                                        String credential = Credentials.basic("jesse", "password1");
                                        return response.request().newBuilder()
                                                .header("Authorization", credential)
                                                .build();
                                    }

        Request request = new Request.Builder()
                .url("http://publicobject.com/secrets/hellosecret.txt")
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }

        });

casual poster
Jun 29, 2009

So casual.
Hey java thread, I need your help with simple java stuff :(
I can't seem to wrap my head around how to add an object to an arrayList. The arraylist and object are both in different classes, I've exhausted and read a bunch of different sources and can't seem to find any help.
Here's the code for the Person class which creates the object

code:
public class Person {
    private String name;
    private String number;
    
    public Person (String name, String number){
        this.name = name;
        this.number = number;
        
        
    }
    
    public String toString(){
        return this.name + " number: " + this.number;
    }
    public String getName(){
        return this.name;
    }
    public String getNumber(){
        return this.number;
    }
    public void changeNumber(String newNumber){
        this.number = newNumber;
    }
}
and here's the Phonebook class where I create the arrayList and add the Person object to it.
code:
import java.util.ArrayList;
public class Phonebook {
   ArrayList <Phonebook> person = new ArrayList <Phonebook>();
   
   
   public void add(String name, String number){
       person.add(new Person(name, number));
       
   }
public void printAll (){
        for (Phonebook members : person)
            System.out.println( members );
            
        }
    }
What the hell am I missing? This stuff should be easy but I'm just missing something from here. I've gone back and reread different sections of my class but can't really find the answer.

Also here's the main program:
code:
public class Main{
public static void main(String[] args) {
    Phonebook phonebook = new Phonebook();
    
    phonebook.add("Pekka Mikkola", "040-123123");
    phonebook.add("Edsger Dijkstra", "045-456123");
    phonebook.add("Donald Knuth", "050-222333");

    phonebook.printAll();
}
}

Kuule hain nussivan
Nov 27, 2008

casual poster posted:

Hey java thread, I need your help with simple java stuff :(
I can't seem to wrap my head around how to add an object to an arrayList. The arraylist and object are both in different classes, I've exhausted and read a bunch of different sources and can't seem to find any help.
Here's the code for the Person class which creates the object

code:
public class Person {
    private String name;
    private String number;
    
    public Person (String name, String number){
        this.name = name;
        this.number = number;
        
        
    }
    
    public String toString(){
        return this.name + " number: " + this.number;
    }
    public String getName(){
        return this.name;
    }
    public String getNumber(){
        return this.number;
    }
    public void changeNumber(String newNumber){
        this.number = newNumber;
    }
}
and here's the Phonebook class where I create the arrayList and add the Person object to it.
code:
import java.util.ArrayList;
public class Phonebook {
   ArrayList <Phonebook> person = new ArrayList <Phonebook>();
   
   
   public void add(String name, String number){
       person.add(new Person(name, number));
       
   }
public void printAll (){
        for (Phonebook members : person)
            System.out.println( members );
            
        }
    }
What the hell am I missing? This stuff should be easy but I'm just missing something from here. I've gone back and reread different sections of my class but can't really find the answer.

Also here's the main program:
code:
public class Main{
public static void main(String[] args) {
    Phonebook phonebook = new Phonebook();
    
    phonebook.add("Pekka Mikkola", "040-123123");
    phonebook.add("Edsger Dijkstra", "045-456123");
    phonebook.add("Donald Knuth", "050-222333");

    phonebook.printAll();
}
}

The diamond brackets after the ArrayList specify what sort of objects go into it. Check your code and think about what you're going to put into your ArrayList.

Carbon dioxide
Oct 9, 2012

Your problem lies in the following two lines:

code:
ArrayList <Phonebook> person = new ArrayList <Phonebook>();
code:
for (Phonebook members : person)
An ArrayList is defined with a generic type. That means, when creating an ArrayList you need to define what type of data it can hold. In this case, the ArrayList needs to hold instances of the Person class. So you need to write ArrayList <Person> person = new ArrayList <Person>();. This makes a list of Persons, while you were trying to make a list of Phonebooks before.

I'd also rename the list from 'person' to 'phonebook' or whatever for clarity, so the line would become:
code:
ArrayList<Person> phonebook = new ArrayList<Person>();
In the foreach loop, you want to say: read each member of the list. Treat them as Person instances, named 'phonebook'
You would do that by writing
code:
for (Person member: phonebook)
So: Out of 'phonebook', read each object, treat it as a Person, and call it member. Then you can use 'member' in the println, because Person has a toString method.

casual poster
Jun 29, 2009

So casual.
Holy crap it worked! Thanks a lot you two!

Kuule hain nussivan
Nov 27, 2008

casual poster posted:

Holy crap it worked! Thanks a lot you two!
Nae worries. How have you liked the MOOC so far?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

casual poster posted:

Holy crap it worked! Thanks a lot you two!

Go back and read the error the message the compiler was giving your before and see if it makes sense to you now.

Ochowie
Nov 9, 2007

What's the current recommendation for REST API frameworks? I'm working on something for a personal project and I'm not sure what to use. I started down the path of using Jersey with Tomcat but I'm not sure if that's the best approach. I'm thinking of looking into spring-boot but the Spring framework kind of scare me.

Edit: Even though it's for a personal project I'd to use it to learn something that would be useful for working on a larger application as well. Also, I've looked at Spark (not Apache Spark) but I'm not sure how mature that is.

Ochowie fucked around with this message at 01:17 on Dec 28, 2015

HFX
Nov 29, 2004

Ochowie posted:

What's the current recommendation for REST API frameworks? I'm working on something for a personal project and I'm not sure what to use. I started down the path of using Jersey with Tomcat but I'm not sure if that's the best approach. I'm thinking of looking into spring-boot but the Spring framework kind of scare me.

Jersey is fine. Spring isn't as scarey as it first seems.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

Ochowie posted:

What's the current recommendation for REST API frameworks? I'm working on something for a personal project and I'm not sure what to use. I started down the path of using Jersey with Tomcat but I'm not sure if that's the best approach. I'm thinking of looking into spring-boot but the Spring framework kind of scare me.

Edit: Even though it's for a personal project I'd to use it to learn something that would be useful for working on a larger application as well. Also, I've looked at Spark (not Apache Spark) but I'm not sure how mature that is.

Jersey is good - I've used it professionally at two companies that were exclusively REST microservices.

Spring is fine - I used it at my last job - but imo it is a bit obnoxious to work with, since it tries to be everything for everyone. At my current place, we use HK2 for dependency injection, and just pass a JSON file to the app at startup with all the configuration that we need. We use GSON to deserialize it into a POJO, and use the HK2 AbstractBinder to register all of the objects/values that we'll need before we start the Jersey server.

Zaphod42
Sep 13, 2012

If there's anything more important than my ego around, I want it caught and shot now.

Ochowie posted:

What's the current recommendation for REST API frameworks? I'm working on something for a personal project and I'm not sure what to use. I started down the path of using Jersey with Tomcat but I'm not sure if that's the best approach. I'm thinking of looking into spring-boot but the Spring framework kind of scare me.

Edit: Even though it's for a personal project I'd to use it to learn something that would be useful for working on a larger application as well. Also, I've looked at Spark (not Apache Spark) but I'm not sure how mature that is.

Jersey is great because its JAX-RS, so if for some reason you have an issue you can swap in another JAX-RS implementation and everything is pretty much the same. But Jersey is the reference implementation so its what seems to get the most active development.

Spring feels like such a major framework, you don't want to use it unless you're using Spring for everything. It isn't "just" a REST library, its a full on framework. Its good but its very much its own thing.

E:FB

Volguus
Mar 3, 2009

Zaphod42 posted:

Jersey is great because its JAX-RS, so if for some reason you have an issue you can swap in another JAX-RS implementation and everything is pretty much the same. But Jersey is the reference implementation so its what seems to get the most active development.

Spring feels like such a major framework, you don't want to use it unless you're using Spring for everything. It isn't "just" a REST library, its a full on framework. Its good but its very much its own thing.

E:FB

JAX-RS is great, until you need something that's not in the specification. Then you go full-Jersey on it. Then you need something that even Jersey cannot handle on its own. Then you go 3rd-party library on it.

Or you can go Spring from the beginning and don't worry about that poo poo. Spring can be as big or as small as you want it, but it usually integrates pretty well with a lot of libraries and with a lot of projects under the Spring umbrella. It is scary if you want to get/understand everything at once, is pretty mild if you take it slow, just chew the pieces you know you can swallow.

Ochowie
Nov 9, 2007

Volguus posted:

JAX-RS is great, until you need something that's not in the specification. Then you go full-Jersey on it. Then you need something that even Jersey cannot handle on its own. Then you go 3rd-party library on it.

Or you can go Spring from the beginning and don't worry about that poo poo. Spring can be as big or as small as you want it, but it usually integrates pretty well with a lot of libraries and with a lot of projects under the Spring umbrella. It is scary if you want to get/understand everything at once, is pretty mild if you take it slow, just chew the pieces you know you can swallow.

What's the best way to get into Spring? I've been reading a little bit about Spring Boot as a way to get everything wired up and running. Is this a good place to start?

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
Yeah, just follow a guide like this one and then afterwards just add any additional features you need.

baquerd
Jul 2, 2007

by FactsAreUseless

Ochowie posted:

What's the best way to get into Spring? I've been reading a little bit about Spring Boot as a way to get everything wired up and running. Is this a good place to start?

Treat it a little like magic. Assume that it does anything you need it to do out of the box with minimal effort, you just need to find (google) the right magic words, such as SimpleBeanFactoryAwareAspectInstanceFactory (http://docs.spring.io/spring-framework/docs/2.0.8/api/org/springframework/aop/config/SimpleBeanFactoryAwareAspectInstanceFactory.html).

After a few months, you might start digging around and seeing how it fits together under the hood, but it's usually not required.

Gravity Pike
Feb 8, 2009

I find this discussion incredibly bland and disinteresting.

baquerd posted:

Treat it a little like magic. Assume that it does anything you need it to do out of the box with minimal effort, you just need to find (google) the right magic words, such as SimpleBeanFactoryAwareAspectInstanceFactory.

Which leads to stuff like this, which highlights the near-impossibility of distinguishing actual Spring magic from random words smooshed together.

speng31b
May 8, 2010

Not in the same space as the frameworks the others are mentioning, but I've worked on smaller projects and microservices with vert.x and it's super fun. http://vertx.io/

Just java - good Java, rxjava, no @InjectBaseReflectiveMagicBeanFactoryImplStrategySingleton to contend with.

Not saying those things are bad. But sometimes you just want to write Java code that does something remotely similar to what it looks like it does.

speng31b fucked around with this message at 04:59 on Dec 29, 2015

Volguus
Mar 3, 2009
Haha, you guys sound like little kids that can't understand the math homework and therefore math is stupid. Is there SimpleBeanFactoryAwareAspectInstanceFactory in spring? Yes. Do you need to know it exists? Most likely not. I've been working with spring for 8 years now, I wrote aspects of every kind imaginable, yet I've never once saw SimpleBeanFactoryAwareAspectInstanceFactory.

Are there classes named like in the http://java.metagno.me/ site? Yes they are. Will you need them? Dunno, most likely not, depends.

I kinda understood the old days fear of the dreaded XML configuration hell, but now you have everything you want via annotations, you just tell it (in plain motherfucking english) what you want and it does it for you. Treat it like magic? Sure, you can I guess, though it doesn't hurt (after you get used to it) to actually understand how it's doing what it's doing. Now you have Spring Boot, which, even easier than before you can bootstrap a simple app in minutes.

Is JAX-RS better? It depends on the taste. Yes it looks OK. If you live within its constraints, is wonderful. You want more? You're hosed and just say gently caress it with the standards and go in the 3rd party libraries. Want pretty Jersey class names? Does ContainerMessageBodyWorkersInitializer look ok? How about AbstractContainerLifecycleListener? They're there, may not need to see them/use them ever, but they're there.

Instead of just looking up class names in the javadoc, how about use it for 5 minutes : https://spring.io/guides/gs/spring-boot/ and then make up your mind.
Otherwise, you really just sound like little kids angry at the toy they don't have. Can't really explain why you're angry (never played with it), but it surely pisses you off that it has a red hat while obviously blue would have been a better choice.

speng31b
May 8, 2010

We're making fun of the words because they're long and funny, which is easy to do with Spring. I don't see anyone recommending to not use Spring because of that, so calm right on down. Did your parents name you BeanFactoryImpl and it's a sore spot? It's a family name? Note I specifically said Spring isn't bad, and qualified that the framework I suggested is probably suited to smaller projects and microservices.

speng31b fucked around with this message at 15:25 on Dec 29, 2015

Jo
Jan 24, 2005

:allears:
Soiled Meat
On the subject of Spring, what's the status and relation between JPA and Hibernate for persistence? The spring.io guide page is oddly devoid of Hibernate mentions. Does JPA supplant Hibernate?

EDIT: :downs: http://stackoverflow.com/questions/9881611/whats-the-difference-between-jpa-and-hibernate

Jo fucked around with this message at 03:14 on Jan 4, 2016

Sedro
Dec 31, 2008

Jo posted:

On the subject of Spring, what's the status and relation between JPA and Hibernate for persistence? The spring.io guide page is oddly devoid of Hibernate mentions. Does JPA supplant Hibernate?
JPA is a specification (JSR). Hibernate is the most widely used implementation of the spec. Spring is an unrelated project.

Theoretically you could replace Hibernate with another JPA implementation and your project would continue to function without any code change. In practice you probably can't.

HFX
Nov 29, 2004

Sedro posted:

JPA is a specification (JSR). Hibernate is the most widely used implementation of the spec. Spring is an unrelated project.

Theoretically you could replace Hibernate with another JPA implementation and your project would continue to function without any code change. In practice you probably can't.

Moving to another implementation is a lot easier then it was during the past. The biggest issue I ran into was were we used a few nice functions that Hibernate supported but Eclipselink did not support.

mehmedbasic
Jul 6, 2015

Sedro posted:

Theoretically you could replace Hibernate with another JPA implementation and your project would continue to function without any code change. In practice you probably can't.

Hibernate has some (now deprecated) annotations that also exist in JPA, so you may have to change some imports. These are largely compatible, as far as I remember.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
I was toying around with Spring Boot and Webjars a little bit and building/minifying my assets using WRO and it works pretty well: In debug I read my source files from disk, in production I get minified assets from the classpath. However, it lacks more modern processors like a JSX compiler or ES6 transpiler (e.g. BabelJS). I'd really like to avoid setting up a terribly fragile Node-based build pipeline based on Gulp or whatever, so I was wondering what you guys are using?

M31
Jun 12, 2012
You can run some of the tooling with Nashorn if you have limited requirements, but yeah, were using a Node and gulp using frontend-maven-plugin. It's actually not very fragile if you use npm shrinkwrap or manage the dependencies yourself, but definitely not ideal.

qntm
Jun 17, 2009
I'm going crazy and I've managed to boil my example code down as far as this:

Java code:
class A {
  String className = "A";
  String getClassName() {
    return this.className;
  }
}

class B extends A {
  String className = "B";
}

class Main {
  public static void main(String[] args) {
    System.out.println(new B().className);           // "B"
    System.out.println(new B().getClassName());      // "A"
  }
}
What is going on here? Why does my B instance apparently have two className member variables, such that the getClassName method grabs the wrong one? What should I be doing instead?

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
You want B's constructor to set your string, rather than declaring an entirely new variable. I'm actually not quite sure how that's compiling, since you're declaring a member variable that already exists. Check for typos, and are you SURE this example reflects your code?

Adbot
ADBOT LOVES YOU

CPColin
Sep 9, 2003

Big ol' smile.
Child classes are allowed to "hide" members from their parent classes. It gets annoying, like in this case. Eclipse, at least, can be configured to warn when a member variable or method parameter is hiding another variable.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply