Important Keywords: Static, Continue,Break:
Static:
The static keyword in java is used for memory management mainly.
The static keyword members belongs to the class rather than instance of the class.
Following members can be declared as static:
A static variable is a class variable and doesn’t belong to Object/instance of the class.
Since static variables are shared across all the instances of Object, they are not thread safe.
Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName
static methods: A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
Usually static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance;
For example : main() method is static and it is the entry point of a java program
static Block: Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.
It is used to initialize static variables of the class.
We can have multiple static blocks in a class, although it doesn’t make much sense.
Static block code is executed only once when class is loaded into memory.
Only static members can be referred in static block.
static Class: We cant use static keyword with class directly on the top level classes.
But we can use static keyword with nested classes.
The nested static class can be accessed without having an object of outer class.
Example:
class OuterClass{
//Static class
static class InnerClass{
static String str="Inside Class InnerClass";
}
public static void main(String args[])
{
InnerClass.str="Inside Class OuterClass";
System.out.println("String stored in str is- "+ InnerClass.str);
}
}
Continue:
The
The unlabeled form skips to the end of the innermost loop's body and evaluates the
Unlike break statement it does not terminate the loop , instead it skips the remaining part of the loop and control again goes to check the condition again.
Example:
for(int i =0; i < 5 ; i++){
for(int j=0 ; j < 5 ; j++){
if(j == 2)
continue;
System.out.println(“i:” + i + “, j:”+ j);
}
In above example, when j becomes 2, the rest of the inner for loop body will be skipped.
Break : Break Statement is generally used to break the loop of switch statement.
Break Statements skips remaining statements and execute immediate statement after loop.
The statements break and continue in Java alter the normal control flow of compound statements. The break and continue statements do not make sense by themselves.
Java does not have a general goto statement. But the statements break and continue take the place of most of uses of the goto. Java does allow any statement to be labeled as in
Static:
The static keyword in java is used for memory management mainly.
The static keyword members belongs to the class rather than instance of the class.
Following members can be declared as static:
- variable (also known as class variable)
- method (also known as class method)
- block
- nested class
A static variable is a class variable and doesn’t belong to Object/instance of the class.
Since static variables are shared across all the instances of Object, they are not thread safe.
Usually static variables are used with final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it as: ClassName.variableName
//static variable exampleprivate static int age;public static String name; |
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
Usually static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance;
For example : main() method is static and it is the entry point of a java program
static Block: Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.
It is used to initialize static variables of the class.
We can have multiple static blocks in a class, although it doesn’t make much sense.
Static block code is executed only once when class is loaded into memory.
Only static members can be referred in static block.
static Class: We cant use static keyword with class directly on the top level classes.
But we can use static keyword with nested classes.
The nested static class can be accessed without having an object of outer class.
Example:
class OuterClass{
//Static class
static class InnerClass{
static String str="Inside Class InnerClass";
}
public static void main(String args[])
{
InnerClass.str="Inside Class OuterClass";
System.out.println("String stored in str is- "+ InnerClass.str);
}
}
Continue:
The
continue statement skips the current iteration of a for, while , or do-while loopThe unlabeled form skips to the end of the innermost loop's body and evaluates the
boolean expression that controls the loop.Unlike break statement it does not terminate the loop , instead it skips the remaining part of the loop and control again goes to check the condition again.
Example:
for(int i =0; i < 5 ; i++){
for(int j=0 ; j < 5 ; j++){
if(j == 2)
continue;
System.out.println(“i:” + i + “, j:”+ j);
}
}
In above example, when j becomes 2, the rest of the inner for loop body will be skipped.
Break : Break Statement is generally used to break the loop of switch statement.
Break Statements skips remaining statements and execute immediate statement after loop.
The statements break and continue in Java alter the normal control flow of compound statements. The break and continue statements do not make sense by themselves.
Java does not have a general goto statement. But the statements break and continue take the place of most of uses of the goto. Java does allow any statement to be labeled as in
label : statementThis is useful only for those compound statements, because break and continue can target a labeled compound statement. In this case they take the form:
break label continue label
Labelled statement examples for continue and break
public class Sample {
public static void main(String[] args) {
OuterLoop: for (int i = 2;; i++) {
for (int j = 2; j < i; j++) {
if (i % j == 0) {
continue OuterLoop;
}
}
System.out.println(i);
if (i == 11) {
break OuterLoop;
}
}
}
}
final and abstract ---
Comments
Post a Comment