GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes The One thing that confuses me in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The One thing that confuses me in Java" Watch "The One thing that confuses me in Java" New topic
Author

The One thing that confuses me in Java

Thomas Knight
Ranch Hand

Joined: Feb 08, 2003
Posts: 33
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

Joined: Oct 29, 2001
Posts: 8915
    
    8

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 ]

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
"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.



java j2ee job interview questions with answers | Learn the core concepts and the key areas
 
GeeCON Prague 2014
 
subject: The One thing that confuses me in Java