The Artima Developer Community
Sponsored Link

Java Answers Forum
which kind of code can access class variables

3 replies on 1 page. Most recent reply: Aug 22, 2002 10:34 AM by junior9000ca

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
jinnjee

Posts: 2
Nickname: jinnjee
Registered: Aug, 2002

which kind of code can access class variables Posted: Aug 21, 2002 5:06 PM
Reply to this message Reply
Advertisement
hi all

in the article at http://www.artima.com/designtechniques/staticP.html --Designing with Static Members, the author says there is <quote>certain kind of code has a special privilege that gives them direct access to private class variables</Quote>

what kind of code is the author taking about?


thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: which kind of code can access class variables Posted: Aug 22, 2002 4:07 AM
Reply to this message Reply
Static methods can only use static variables in the class so the only "code" that can use private variables in a class is any method that is not static and belongs to that perticular class. All other classes can't use the private variables. If the variable is public then any class can make reference to it.

jinnjee

Posts: 2
Nickname: jinnjee
Registered: Aug, 2002

Re: which kind of code can access class variables Posted: Aug 22, 2002 9:57 AM
Reply to this message Reply
from the article, i gather that...

1. instance variables and instance methods mean non-static variables and methods which apply to each unique instance
2. Class variables and Class methods mean static variables and methods which apply to the class as a whole.

if i understood the above things right, i can say that...

Static variables are nothing but class variables.

am i correct so far?

junior9000ca

Posts: 2
Nickname: junior9000
Registered: Aug, 2002

Re: which kind of code can access class variables Posted: Aug 22, 2002 10:34 AM
Reply to this message Reply
I don't understand your direct problem but hope this helps as a start:

variable types:
1.instance- Variables declared at the global(outside of methods)level and can be accessed from any method within a class and possiable subclasses. These are also known as class variables.

2.private variables-Variables which can only be accessed by members(ex: methods) of the class and not its sub classes

3.static variables-a variable which is shared among all objects of a class and possiable sub classes. A change to the value of a variable will change a value to all the objects containing this variable

4.public- are accessed by all members of the class and subclasses.This is the default definition

5.protected- are accessed only by the class and its associated subclasses

6.final- variables which can not be changed of their initial value

thus class variables or instance variables can be accessed only by their associated definitions such as public, private and protected (these are encapsulation or hiding definitions). hence the rules above state what code and how far a variable can be reached and thus access.

If you have any further questions please reply.

Flat View: This topic has 3 replies on 1 page
Topic: Chat room Previous Topic   Next Topic Topic: how do I make jar files

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use