Solved

quick easy points!  very simple question about class variables!

Posted on 2006-05-04
4
196 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 100 total points
Comment Utility
> 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
Comment Utility
God, I knew it was something that simple!   Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now