Solved

quick easy points!  very simple question about class variables!

Posted on 2006-05-04
4
203 Views
Last Modified: 2010-03-31
This is one of those little questions that bugs the hell out of me, but I always felt like I should know the answer to it without much though.  I gave up being frustrated about it, so here's the question ....  If I'm writing a class that is seperate from the driver, it seems that when I declare the variables outside of the constructor or any other method, I can access them without having to declare them static.  If I'm working with the main class that has the driver in it, and I declare the variables in the same manner, I'm unable to access them as I normally would unless I declare them static.  

Why is this?  

50 points seems barely worthwhile, so I made the answer worth 100 points!   :)
0
Comment
Question by:trs28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 100 total points
ID: 16608331
> I can access them without having to declare them static
an instance has access to its static and non-static members

>  If I'm working with the main class that has the driver in it, and I declare the variables in the same manner, I'm unable to access them as I normally would unless I declare them static.  

because main() is static and a static method can only access static fileds

class A {
    String s1;
    static String s2;

    f() {
        s1, s2 are accessible
    }

    static g() {
        only s2 is accessible
    }
}
0
 

Author Comment

by:trs28
ID: 16608347
God, I knew it was something that simple!   Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16608379
>>
it seems that when I declare the variables outside of the constructor or any other method, I can access them without having to declare them static.
>>

Variables declared *inside* the ctor or any other method are local to that ctor or method and cannot be accessed outside it
0
 

Author Comment

by:trs28
ID: 16608412
yeah, i knew that one CEHJ, but It was the main method that was throwing me off on the whole encapsulation thing!  Thanks though!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question