Solved

quick easy points!  very simple question about class variables!

Posted on 2006-05-04
4
202 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 264
glassfish admin console 1 53
junit example issue 2 59
Html split(text) 2 50
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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