Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same keyword for two purposes in java?

Tags:

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

like image 972
GuruKulki Avatar asked Mar 08 '10 15:03

GuruKulki


People also ask

What is double keyword in Java?

The double keyword is a data type that can store fractional numbers from 1.7e−308 to 1.7e+308.

Can we have 2 methods with same class in Java?

For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.

What is static keyword in Java?

The static keyword is a non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class.

What are keywords give any two examples in Java?

Examples of keywords are the primitive types, int and boolean ; the control flow statements for and if ; access modifiers such as public , and special words which mark the declaration and definition of Java classes, packages, and interfaces: class , package , interface .


3 Answers

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)

like image 192
Bozho Avatar answered Oct 17 '22 09:10

Bozho


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.

like image 29
Jon Skeet Avatar answered Oct 17 '22 09:10

Jon Skeet


  • 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)
like image 4
Wouter Coekaerts Avatar answered Oct 17 '22 09:10

Wouter Coekaerts