File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Declaring variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaring variables." Watch "Declaring variables." New topic
Author

Declaring variables.

Joy Vergis
Ranch Hand

Joined: Sep 14, 2009
Posts: 45
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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".


Mohamed Sanaulla | My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38835
    
  23
You will get an error later if you try to use name and find it points to null.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38835
    
  23
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38835
    
  23
Hex2019 is the posh version of apostrophe. Posh quotes are 201c and 201d.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38835
    
  23
Møø

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 12, 2011
Posts: 36

It is related to the scope, local scope overrides the global scope here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring variables.