This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why reference does not pass default values?  RSS feed

 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!