File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring variables.

 
Joy Vergis
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a new bee to java. I am trying out a sample program as below. Why I dont get any error when i declare the same variable in the constructor.






Please help me under stand this concept.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scoping is different for the two variables. The one declared with in the class is the instance level and the one declared in the constructor is local to the constructor. The one in the constructor shadows the one in the class. You might want to read more about this by searching for "shadowing variables".
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get an error later if you try to use name and find it points to null.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You will get an error later if you try to use name and find it points to null.


<nitpick>
1. That depends on how he tries to use it.

2. It won't point to null; it will hold the value null.
</nitpick>

Sorry, couldn't help myself. :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He’s a she. She can’t use it; she can only do things which don’t mind its being null. Things like System.out.println(null); don’t so much use the nul as test for it and substitute the String "null" for it. Of course, you can always assign from it and spread the nice nullness throughout your whole application.
I’ve got to have the last word., haven’t I

Agree, it contains rather than points to null.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:He’s a she.


Oops. Didn't even pay attention. Apologies.

She can’t use it


Sure she can. She can use the variable as a flag to indicate that the object has not been created/initialized.

; she can only do things which don’t mind its being null. Things like System.out.println(null);


Well, I didn't say they were good or common uses.

(Side note, you seem to have a funky quote in your post: I'm seeing HTML (XML?) codes for hex 2019.)

I’ve got to have the last word., haven’t I


Oh, so it's a battle of wills then, is it Trebek?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hex2019 is the posh version of apostrophe. Posh quotes are 201c and 201d.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Hex2019 is the posh version of apostrophe. Posh quotes are 201c and 201d.


Quote, apostrophe, Møøse, it's all the same to me.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Møø

Maybe we should allow this thread to return to its original purpose?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Møø

Maybe we should allow this thread to return to its original purpose?


Quite.

@Joy: Your question was answered in the first post. Disregarding the tangent I dragged us onto, do you understand now, or are you still confused?
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is related to the scope, local scope overrides the global scope here.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic