aspose file tools*
The moose likes Beginning Java and the fly likes remembering access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "remembering access modifiers" Watch "remembering access modifiers" New topic
Author

remembering access modifiers

Trip Artrip
Greenhorn

Joined: Jan 11, 2002
Posts: 1
Does anyone have any tricks or tips for how they remember which modifier does what? I can't keep them straight in my head. The ones I have trouble with are: "final", "abstract", and "static".
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
Trip,
Maybe there are tricks or tips to remember them but this is so elementary and so much used (if not always) I shouldn't try to remember them by a trick. IMHO these basics have to be really understood thoroughly... but again, that's my opinion.
Erik Dark
[ February 13, 2002: Message edited by: Erik Dark ]
Marius BONG
Greenhorn

Joined: Dec 16, 2001
Posts: 13
Do as much as possible excersice. that would help you much i think. Learning java is not only memorising but experience. And the experience come from the exercise that we have done. The more exercise the more experience we have (the more we learn). And I think the most important to learn java is Experience, that's all..
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

One of the things that always confused me, is that if you wanted to be REALLY picky, none of those things are "real" access modifiers. The only 'true' **access** modifiers are

public, private, protected (and package, or 'default')

final, static and abstract are also modifiers, but only static has 'access' ramifications in the sense of the other 'access modifiers', and then only if you look at it sideways. The modifiers you list are more architectural, in my mind. They describe qualities of the classes/methods/variables that they modify that are beyond mere 'access'.

final 'things' can't be changed. For variables, that means they are constant. Final methods can't be overriden and final classes can't be subclassed.

abstract can only be applied to methods and classes. It means that there is no implementation for the method or class which it is modifying.
**
I've thought about that a bit more... There is implementation possible in an abstract class. But if any single method is abstract, then the whole thing (class) is abstract. So it might be better to think of it as: An abstract class cannot be instantiated.
**

static means that the variable or method belongs not to an instance of the class, but rather to the class as a whole. So this implies that you don't need an instance to use a static variable or method.
[ February 13, 2002: Message edited by: Mike Curwen ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I don't think there is *anything* picky about pointing out that there are only 3 access modifiers: public, protected, and private.
There are lots of other modifiers, but they are not "access modifiers." Only access modifiers modify the accessibility of a member.


Rob
SCJP 1.4
Marius BONG
Greenhorn

Joined: Dec 16, 2001
Posts: 13
I don't think there is *anything* picky about pointing out that there are only 3 access modifiers: public, protected, and private.
How about the rest ? what modifiers are they ?
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
There's not always an either-or way of labeling things. There's overlapping set membership of lots of java terms. Like the concept of reserved words and keywords that seem to cause so much confusion.
There are several ways of organizing the modifiers. First, by what they can modify:
classes, constructors,fields, interfaces, and methods.
Not all modifiers can be used together, and certain combinations aren't allowed either
class modifiers : public protected private abstarct static final strictfp
constructor modifiers : public protected private
field modifiers: public protected private static final transient volatile
interface modifiers: public protected private abstract static strictfp
method modifiers : public protected private abstract static final synchronized native strictfp
Notice how you see "public protected private" in all categories. That's because they serve to control what members can be accessed by another object in any given context; they are the access modifiers. That's why they get a special name as a group.
But all of these keywords are called "modifiers" because they modify classes, constructors,fields, interfaces, or methods:
abstract final native private protected public static synchronized transient volatile.
[ February 14, 2002: Message edited by: Rob Ross ]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The way that I always understood it is:
The access specifiers are:
private, protected, public and default ("friendly" or "package")
The access modifiers are:
abstract and final
Declaration modifiers:
transient, volitile and synchronized
Based on this "static" is not really a modifier at all. It is a definition of what a thing is - not how available it is. A static variable is a completely different thing than a member variable. It lives in a different place. It has a different lifecycle. etc.
[ February 15, 2002: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well, the JLS doesn't have a term "access specifier," but it does have the term "access modifier."

8.5.1 Access Modifiers
The access modifiers public, protected, and private are discussed in �6.6.
A compile-time error occurs if a member type declaration has more than one of
the access modifiers public, protected, and private.

There are also several places in the JLS that include the static keyword in the definition of an access modifier:

(last sentance of 7.6)
It is a compile-time error if a top level type declaration contains any one of the
following access modifiers: protected, private or static.


8.1.1 Class Modifiers
...The access modifier static pertains only to member classes (�8.5, �9.5)...

But there is no term "access specifier" in the JLS.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Which is why I pointed out that this leads to some confusion.

'access' has too many subtle meanings, and depending on how you look at things, static is sometimes an access-type modifier. ie: You can 'access' the variable without an instance of the class.

They should have called them 'scope modifiers'.

As for what the other ones are, I'd just call them 'modifiers'
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
So you think that we should put in a request to Sun that they clean up their terminology??
Sure - YOU write it and I'll sign it.
So exactly WHO would we direct that to???
Marius BONG
Greenhorn

Joined: Dec 16, 2001
Posts: 13
forgive me for my question, what is JLS ?
Erik Dark
Ranch Hand

Joined: Jan 28, 2002
Posts: 107
Java Language Specification
Erik Dark
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Just to be clear - the term Access Specifiers is used all over in the Sun Tutorials.
Ex: from The Java Tutorial
In Java, you can use access specifiers to protect both a class's variables and its methods when you declare them. The Java language supports four distinct access levels for member variables and methods: private, protected, public, and, if left unspecified, package.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remembering access modifiers