File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

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...
Danish
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: http://aspose.com/file-tools
 
subject: Strings