wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why reference does not pass default values? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why reference does not pass default values?" Watch "Why reference does not pass default values?" New topic
Author

Why reference does not pass default values?

Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
I have a class,

class Student
{
String name;

Student(String s)
{
name=s;
}

void display()
{
System.out.println("name is "+name);
}
}



If a reference is created for the above class without allocating memory ie Student s & not Student s=new Student

and its method is called s.display(), why the null value or the default values(in general) are not passed ?

java throws exception "s might not have been initialised".


thanks
Jyo
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Only member variables are initialized with default values (null in the case of a reference variable). Local variables (inside a method) must be explicitly initialized, and if you don't, you'll get a compiler error "variable might not have been initialized".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jyoti Vaskar
Ranch Hand

Joined: Jun 30, 2009
Posts: 142
Jesper Young wrote:Only member variables are initialized with default values (null in the case of a reference variable). Local variables (inside a method) must be explicitly initialized, and if you don't, you'll get a compiler error "variable might not have been initialized".


thanku .

I, now have understood it clearly .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why reference does not pass default values?
 
Similar Threads
Key Advantages of Java 5
getFirstName from one class (set & get) to another class (Main Program)
Need help with my Lottery Program
Arraylist and for enchanced loop question
hashmap