Important Java Keywords That You Must Know
Check out the most important keywords in Java!
Join the DZone community and get the full member experience.
Join For FreeWhat Is a Keyword in Java?
Java keywords are a reserved word that has a special meaning associated with it. For easy identification, they are usually highlighted in Java. Out of 50 keywords, 48 are in use while two are not.
Let us study some of the important Java keywords in greater detail.
Important Java Keyword List
- abstract: It utilizes to accomplish abstraction. It is a non-access to modifier that is relevant for classes and methods.
- enum: It utilizes to characterize enum in Java.
- instanceof: It is utilized to know whether the object is a case of the predefined write (class, subclass, or interface).
- private: It is an access modifier. Anything proclaimed private can’t be seen outside of its class.
- protected: If you need to enable a component to see outside your present class, however, just to classes that subclass your class specifically, at that point announce that component ensured.
- public: Anything proclaimed openly get to from any place.
Access Modifiers
- static: It is utilized to make a member (block, method, variable, or nested class) that can utilize independent from anyone else, without reference to a particular case.
- strictfp: It is utilized for confining, floating, point figuring and calculations, and guaranteeing the same outcome on each stage while performing tasks in the floating point variable.
- synchronized: It is utilized to get synchronization in Java that is applicable to blocks and methods.
- transient: Factors modifier utilized as a part of serialization. At the season of serialization, in the event that we would prefer not to spare estimation of a specific variable in a record, at that point, we utilize the transient keyword.
- volatile: This is used if the modifier tells the compiler that the variable adjusted by an unstable change startlingly via different parts of your program.
Types of Java Keywords
Enum
Enumerations effectively represent a gathering of named constants in a programming language. For instance, the four suits in a deck of playing cards might have four enumerators named Club, Diamond, Heart, and Spade, having a place with an identified sort named Suit. Different illustrations incorporate normal identified composes (like the planets, days of the week, hues, bearings, and so forth).
Java enums utilize when we know every single conceivable value at compile time. For example, this can include decisions on a menu, adjusting modes, summon line banners, and so on. It isn’t vital that the arrangement of constants in an enum write remain settled forever.
Declaration of Enum in Java
- Enums cannot declare inside a method, but they can declare inside or outside a class.
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
// inside a class.
public class Test
{
enum Color
{
RED, GREEN, BLUE;
}
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
In the first place, the line inside the enum ought to have a rundown of constants and, afterwards, different things like methods, variables, and constructors. As indicated by Java naming traditions, it suggests that we name consistent with every single capital letter.
Imperative Purposes of Enum
- Each enum inside is executed by utilizing the Class.
- Each enum steady speaks to a question of a sort of enum.
- The enum sort can pass as a contention to switch proclamation.
import java.util.Scanner;
enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
public class Test
{
Day day;
public Test(Day day)
{
this.day = day;
}
public void dayIsLike()
{
switch (day)
{
case MONDAY:
System.out.println("Mondays are bad.");
break;
case FRIDAY:
System.out.println("Fridays are better.");
break;
case SATURDAY:
case SUNDAY:
System.out.println("Weekends are best.");
break;
default:
System.out.println("Midweek days are so-so.");
break;
}
}
public static void main(String[] args)
{
String str = "MONDAY";
Test t1 = new Test(Day.valueOf(str));
t1.dayIsLike();
}
}
Constants in enum are always public static and final. We cannot have child enums as we use finale.
- We can declare the main method inside the enum, and hence, we can use a command prompt to call it.
enum Color
{
RED, GREEN, BLUE;
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
Enum and Inheritance
- All enums verifiably broaden java.lang.Enum class. As a class can just broaden one parent in Java, an enum can’t expand whatever else.
- The
toString()
technique is overridden in java.lang.Enum class, which returns enum consistent name. - Enum can execute numerous interfaces.
Values(), Ordinal(), and ValueOf() Techniques
- These methods are available inside java.lang.Enum.
-
Values()
technique utilize to restore all qualities exhibit inside enum. - An order is essential in enums. By utilizing the
ordinal()
method, each enum steady file can discover much the same as cluster file. - The
valueOf()
technique restores the enum steady of the predefined string values, if it exists.
enum Color
{
RED, GREEN, BLUE;
}
public class Test
{
public static void main(String[] args)
{
Color arr[] = Color.values();
for (Color col : arr)
{
System.out.println(col + " at index "
+ col.ordinal());
}
System.out.println(Color.valueOf("RED"));
}
}
Output:
RED at index 0
GREEN at index 1
BLUE at index 2
RED
Enum and Constructor
- An enum can contain a constructor, and it execute independently for each enum consistent with the constant of enum class loading.
- We can’t make enum questions unequivocally and consequently, and we can’t summon enum constructors specifically.
Enum and Methods
- An enum can contain concrete methods just i.e. no any theoretical technique.
enum Color
{
RED, GREEN, BLUE;
private Color()
{
System.out.println("Constructor called for : " +
this.toString());
}
public void colorInfo()
{
System.out.println("Universal Color");
}
}
public class Test
{
public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
c1.colorInfo();
}
}
Output:
The Constructor called for: RED
Constructor called for: GREEN
Constructor called for: BLUE
RED
Universal Color
Strictfp Keyword
The strictfp is a keyword in Java that utilizes limiting the floating point values and guaranteeing the same outcome on each stage while performing tasks in the coasting point variable.
Floating point computations are platform subordinate, i.e. distinctive output (floating-point variables) accomplish when a class document is kept running on various platforms (16/32/64 bit processors). To settle these kinds of issues, the strictfp catchphrase was presented in the JDK 1.2 form by following IEEE 754 guidelines for coasting point counts.
Important Focuses of Strictfp
- The strictfp modifier utilizes the classes, interfaces, and techniques as they were.
- Strictfp can’t utilize with unique strategies. Notwithstanding, it utilizes with theoretical classes/interfaces.
- Since techniques for an interface are certainly dynamic, strictfp can’t utilize with any method inside an interface.
public class Test
{
public strictfp double sum()
{
double num1 = 10e+10;
double num2 = 6e+08;
return (num1+num2);
}
public static strictfp void main(String[] args)
{
Test t = new Test();
System.out.println(t.sum());
}
}
So, this was all for now about the important Java keywords. Hope you enjoyed!
Published at DZone with permission of Rinu Gour. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments