wood burning stoves*
The moose likes Beginning Java and the fly likes Where to declare a variable?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where to declare a variable??" Watch "Where to declare a variable??" New topic
Author

Where to declare a variable??

Jennifer Bhamoo
Ranch Hand

Joined: Mar 09, 2002
Posts: 47
Hello - I'm new here, so please bear with me...
Trying to figure out the difference between declaring variables in the following areas:
  • "class-level" (instance/class variables)
  • within default constructor

  • I think it starts to really matter when I start extending the class, since a subclass will have it's own default constructor, so if I want particular variables to be accessible to all subclasses, then they need to be declared at the "class-level". Just need a little reassurance of this though.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61646
        
      67

    Absolutely. Any variable that is declared in a method, including a constructor, is available only within the scope of that method. You can initialize the value of variables in a constructor (indeed, that's one of their primary purposes), but the variables must be declared as class or instance variables to be accessible to methods of the class or derived classes.
    hth,
    bear


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Jennifer Bhamoo
    Ranch Hand

    Joined: Mar 09, 2002
    Posts: 47
    Thanks a bunch for the reply (and so quick too!!)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Where to declare a variable??