开发者

Same keyword for two purposes in java?

As we use "default" keyword as a access specifier, and it can be used in switch statements as well with complete different purpose, So i was curious that is there any other 开发者_开发问答keywords in java which can be used in more then one purposes


The "default" in the case of access modifier isn't a keyword - you don't write:
default void doSomething()

However, when specifying the default value of an attribute of annotations - it is.

switch (a) {
   default: something();
}

and

public @interface MyAnnotation {
    boolean bool() default true;
}

That, together with final as pointed out by Jon Skeet seems to cover everything. Perhaps except the "overloaded" for keyword:

for (initializer; condition; step) and for (Type element : collection)


You can't use default as an access specifier, so I don't think even that counts. (EDIT: As Bozho pointed out, it can be used in annotations.)

final means "can't be derived from / overridden" and "is read-only" which are two different - but related - meanings.


  • default can be used both in a switch and as a default value in an annotation (as pointed out by Bozho)
  • final means "can't be derived from / overridden" and "is read-only" which are two different - but related - meanings (as pointed out by Jon)
  • extends can be used both to specify the supertype of a class and can be used in wildcards and type variables to put a constraint (related but not exactly the same) (List<? extends Foo>)
  • super can be used to specify to something in a superclass of the current class, or in a wildcard to put a constraint (List<? super Foo>)
  • static means both "part of the class, not an instance" (for methods, attributes or initializers) and as a static import
  • class to declare a class (class Foo {}), or to refer to a class literal (Foo.class) (as answered by ILMTitan)
  • (for can be used in a normal for loop and the "enhanced" for, but that's more like overloading (as Bozho puts it so nicely) than really having two meanings)


Something no one else has mentioned yet: the class keyword has two different uses.

Declaring a class:

class Test{};

and indicating a class literal:

Class<Test> testClass = Test.class;


The final keyword can mean different things.

  • When modifying classes is means that the class cannot be subclassed.
  • When modifying a method, it means that the method cannot be Overridden.
  • When modifying a variable, it means that the variable cannot point to any other variable.


The default keyword is not used as an access specifier. The absence of private, protected and public means use of default.

Example:

class Test { // default access for class.

 int A; // default access for the class member.
}

Some examples of Java keywords which find different use are:

  • final : A final class cannot be subclassed, a final method cannot be overridden, and a final variable can occur at most once as a left-hand expression.

  • Super: Used to access members of a class inherited by the class in which it appears, also used to forward a call from a constructor to a constructor in the superclass.

  • Static: Used to create static initialization blocks, also static members and static imports.

  • for:Used for the conventional for loop and the newer Java 1.5 enhanced for loop.


The static keyword associates methods and fields with a class instead of instances of that class, but it's also used to signify static initialization sections as in:

public class MyClass
{
   private static int a;

   static
   {
      a = 1;
   }

   public static void doSomethingCool()
   {
      ...
   }
}

Pascal's comment reminded me of static imports:

import static MyClass.doSomethingCool;

public class MyOtherClass
{
   public void foo()
   {
      // Use the static method from MyClass
      doSomethingCool();
   }
}


I gave a look at java keywords but it seems that keywords are unique.. you can check yourself.

By the way default can't used as an access specifier, it's inherited when noone is specified.


Do we really use default as an access specifier? No specifier at all is "default". But you don't use the keyword that way.


final has different uses:

  • in a variable declaration it means a variable can't be changed.
  • In a method signature it means a method can't be overridden
  • In a parameter list it means a variable can't be altered in a method.


The "extends" keyword can be for single inheritance (either implementation or "pure abstract class" aka "interface inheritance" in Java).

The "extends" keyword can also be used for multiple (interface) inheritance.

The ones who always argue that Java doesn't support multiple inheritance will hence have a hard time arguing that "extends" in those two cases is doing exactly the same thing.

Now I'm in the other camp: I consider that multiple interface inheritance is multiple inheritance and that implementation inheritance is just an OOP detail (that doesn't exist at the OOA/OOD level) and hence I consider that "extends" is really doing the same thing in both case and that hence my answer doesn't answer the question :)

But it's an interesting keyword nonetheless :)


You can think of the following things

  1. Default
  2. final
  3. super
  4. ":" (colon) used at different places , which has a different meaning at different places


As all the other answers have stated, there are many keywords that server multiple purposes depending on context. I just wanted to add that there is a reason for this: There is a strong aversion to adding keywords because such additions break existing code, so when new features are added existing keywords are used if they make a reasonable fit, such as super and extends for generics and default for annotations, or they are just skipped as in the colon used in the enhanced for loop.

So my point is to expect that as the language continues to evolve even more uses are found for existing keywords rather than introducing new ones.


BTW there is no such thing as an access specifier in Java. The term in the JLS is 'access modifier'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜