This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic


Danish Shaukat
Ranch Hand

Joined: Nov 16, 1999
Posts: 340
Hi all
I am decaring a String without initialization.
My book says if such a program is compiled the compiler gives an error.
But this compiles well.
public class Foo
private static String h;
public static void main(String[] ards)
System.out.println("String is..."+h);

Now interesting thing is that the followng code does not compile.
public class Foo

public static void main(String[] ards)
String h;
System.out.println("String is..."+h);


So whats the truth about this...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In the first case you are declaring a class variable, and in the second you are declaring a local variable. class variables are initialized for you by the compiler, local variables are not.
member variables are also initialized for you by the compiler.
To learn more about class, member, and local variables and their initilization you will have to read your Java book.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Strings
Similar Threads
How to compile args[ ]?
Why it is illegal to use abstract and static modifiers together in method declaration ?
compiler not giving error
main() is the entry point
SCJP6 book page 298 errata