This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings  RSS feed

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!