This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Declaring variables.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get an error later if you try to use name and find it points to null.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is related to the scope, local scope overrides the global scope here.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic