Skip to main content

Important Keywords: Static, Continue,Break:

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:
  1. variable (also known as class variable)
  2. method (also known as class method)
  3. block
  4. nested class
 static variables: We can use static keyword with a class level variable.
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 example
private static int age;
public static String name;

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 continue statement skips the current iteration of a for, while , or do-while loop
The 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 : statement
This 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

Popular posts from this blog

OBJECT class in Java

OBJECT class in Java : Object is at the top of class hierarchy in java. Every class in the Java system is a descendent (direct or indirect) of the Object class. The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class. Mainly below methods are provided by Object class : public String toString() returns the string representation of this object. protected Object clone() throws CloneNotSupportedException creates and returns the exact copy (clone) of this object. public boolean equals(Object obj) compares the given object to this object. public int hashCode() returns the hashcode number for this object. public final Class getClass() returns the Class class object of this object. The Class class can further be used to get the metadata of ...

Java Priority Queue sort using lambda expression

Priority Queue :     In Java  Priority Queue  is a  queue  which keeps its elements sorted as per their natural order( example in ascending orders for Integer, alphabetical a-z for alphabets) or using a custom Comparator at the time of creation you can custom sort it. It has most method  similar to a queue  add, clear, poll, peek As priority queue have to compare elements to keep in order so elements must be comparable otherwise, it will throw ClassCastException .  As null can not be compared so you are not allowed to insert null too A program to sort priority queue using Lambda function in Java           c lass   PriorityQueueLambdaJava { public List<Integer> KFrequent( int [] nums, int k) { Map<Integer, Integer> map = new HashMap<>(); for ( int j: nums) { map .put(j, map .getOrDefault(j, 0 ) + 1 ); } PriorityQueue<Map.Entry<Integ...