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 double keyword is a data type that can store fractional numbers from 1.7e−308 to 1.7e+308.
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.
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.
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 .
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)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With