Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Read that refernce variables are automatically assigned default value??  RSS feed

 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just read that default values are assigned a default value and just wrote a simple program

class Demo
{
}
class TestD
{
public static void main(String args[])
{
Demo d;
System.out.println(d);
}
}

but it is giving error that d has not been initialised???
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to read again.

Member variables are assigned default values.
The variable declaration:


is a local variable to the main() method and it needs to be initialized before usage.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the main method, after declaring d, no reference is assigned. So when you
pass d to println, and toString is called on d, there is no object to call the method on.

Perhaps you are thinking of instance variables, which are assigned a default
value (0 for integer types, false for boolean, and the null string for String).
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
default value assigned only for instance variables....not for method local variables....

in your code,d is local to main method.. and hence not initialized....if you declare that as class variable then it would be initalized to "null"..

like-

class TestD
{
Demo d;
public static void main(String ar[])
{
System.out.println(d); //print null
}


}
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TestD
{
Demo d;
public static void main(String ar[])
{
System.out.println(d); //print null
}


}

in this case isnt it necessary to make reference variable static
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More precisely, default values are assigned to fields, both instance and static. Java Language specification.
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but instance variables are initialised when objects are created??
 
Campbell Ritchie
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance fields are initialised to their default values when an instance is created, yes, and you ought to initialise them properly in a constructor. The exception is final fields, which must be initialised once and once only, often in a constructor.
 
Just the other day, I was thinking ... about this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!