Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

quick easy points!  very simple question about class variables!

Posted on 2006-05-04
4
Medium Priority
?
205 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
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 …
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

719 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