Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

quick easy points!  very simple question about class variables!

Posted on 2006-05-04
4
Medium Priority
?
206 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
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

926 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