• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remembering access modifiers

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marius BONG
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
forgive me for my question, what is JLS ?
 
Erik Dark
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Language Specification
Erik Dark
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic