• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The One thing that confuses me in Java

 
Thomas Knight
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been programming in Java for a couple of years now, on and off. I really like the language and most things, after spending time with them, seem sensible. There is one thing that I run accross that confuses me and I was wondering if someone could help me.

I know there are primitive type variables and others that are instances of a class(objects). SO if I wanted to declare and initialize an int variable I would do . If I wanted to declare a variable that is not a primative, like say a label in swing I would do something like .

1.) What I don't understand is sometimes in Java code(that compiles) I see Strings declared and initialized like this . How is this possible? String is a class and shouldn't instances of it look something like this ?

2.) Also the same lines I have seen stuff like this . Shouldn't this also be declared with the new operator? Also the method getContentPane() is being called with no class or object name in front of it.
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Knight:


1.) What I don't understand is sometimes in Java code(that compiles) I see Strings declared and initialized like this . How is this possible? String is a class and shouldn't instances of it look something like this ?

It's called a "literal". We get a shorthand way of initalizing a String instance and the compiler does the work of creating the object. Have a glance at the Java Tutorial page on String objects.

Originally posted by Thomas Knight:

2.) Also the same lines I have seen stuff like this . Shouldn't this also be declared with the new operator? Also the method getContentPane() is being called with no class or object name in front of it.


There's an implicit "this" reference in front of the call to getContentPane(). You are actually invoking a method on that object's superclass. Again, have a gander at the Java page on Tutorial the "this" keyword .
[ March 13, 2006: Message edited by: Joe Ess ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Knight:
1.) What I don't understand is sometimes in Java code(that compiles) I see Strings declared and initialized like this . How is this possible? String is a class and shouldn't instances of it look something like this ?


"Bye" is a String literal - it's the only literal defined in the Java language that is an object.

new String("Bye") really is creating a String "Bye", and passes that String to the String constructor. If the compiler wouldn't understand that "Bye" is a String object, even that wouldn't be possible (as well as new JLabel("Bye")).


2.) Also the same lines I have seen stuff like this . Shouldn't this also be declared with the new operator?


No, that's just a method call. The code doesn't directly create a new object, it just retrieves one that is created elsewhere (and in this case, much earlier).

Also the method getContentPane() is being called with no class or object name in front of it.


Yes. The compiler automatically interpretes that as "this.getContentPane()". It's syntactic sugar.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"new" operator creates a new instance of a class like



You cen also do some thing like



in the above situation, you are not creating a new object but pointing your reference "label" to whatever reference returned by 'getAreferenceToAlreadyCreatedLabel()'. So the method 'getAreferenceToAlreadyCreatedLabel() is responsible for creating a new Object and pass the reference to the calling method.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic