• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic