Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Danish
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic