Friday, November 17, 2017

Java Static Keyword | Variable | Method | Block with Java Nested and Inner Class

1. Java Static Keyword

The static keyword in java is used for memory management especially. we can follow java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the elegance than an example of the magnificence.
The static can be:
  • Variable (also known as a class variable)
  • Method (also known as a class method)
  • Block
  • Nested and Inner class

2. Java Static Variable

it's miles a variable which belongs to the magnificence and not to object(instance) Static variables are initialized simplest once, at the start of the execution. those variables can be initialized first, earlier than the initialization of any example variables. An unmarried replica to be shared through all times of the magnificence.
Example
class my
{  
            int rollno;  
            String name;  
            static String college ="VMC";  
            my(int r,String n)
           {  
               rollno = r;  
               name = n;  
           }  
void display ()
           {
               System.out.println(rollno+" "+name+" "+college);
           }  
          public static void main(String args[])
          {  
               my m1 = new my(101,"Dhiren");  
               my m2 = new my(102,"Bhavesh");  
   
               m1.my();  
               m2.my();  
          }  
}  

Output
101 Dhiren    VMC
102 Bhavesh  VMC

3. Java Static Method

if you practice static key-word with any approach, it's far known as static technique.

  1. A static technique belongs to the magnificence instead of an item of a class.
  2. A static approach can be invoked without the need for creating an example of a category.
  3. The static approach can get admission to static records member and may change the cost of it.
Static methods in java belong to the elegance (now not an instance of it). They use no instance variables and will generally take enter from the parameters, perform movements on it, then return a few results. times strategies are associated with objects and, as the call implies, can use instance variables

Example
  class my
{  
     int rollno;  
     String name;  
     static String college = "VMC";  
       
     static void change()
     {  
                 college = "BDP";  
     }  
  
     my
(int r, String n)
    {  
                 rollno = r;  
                 name = n;  
     }  
  
    void display ()
    {
                 System.out.println(rollno+" "+name+" "+college);
    }  
  
   public static void main(String args[])
   {  
                my.change();  
  
                my m1 = new my(101,"Dhiren");   
                my m2 = new my (102,"Hiren");  
                my m3 = new my (103,"Dhir");  
  
  
  my1.my();  
    my2.my();  
    my3.my();  
    }  
}  

Output
101 Dhiren BDP
102 Hiren   BDP
103 Dhir     BDP

4. Java Static Block

Static block is used for initializing the static variables. This block receives accomplished whilst the elegance is loaded into the memory. a class will have more than one Static blocks so one can execute inside the identical sequence wherein they had been written into the program.

Is used to initialize the static records member.

It's miles accomplished before the main method at the time of classloading.

Example
class my
{  
    static
    {
             System.out.println("static block");
    }  
  public static void main(String args[])
   {  
             System.out.println("Hello friends");  
   }  
}  

Output
Static block
hello friends

5. Java Nested and Inner Class

while you are defining a category inside another elegance it's miles called nested class.
 If class B is defined inside class A, then B is understood to A, however not outdoor of A. A nested class has got right of entry to the participants, such as private contributors, of the class wherein it's miles nested.

There are varieties of nested instructions: static and non-static.
A statically nested magnificence is one which has the static modifier applied. due to the fact it is static, it has to get admission to the members of its enclosing elegance through an item.
 An internal magnificence is a non-static nested magnificence. It has get entry to all of the variables and strategies of its outer magnificence and can refer to them at once

Advantages of Nested Class

  1. it's miles a manner of logically grouping classes which are most effective used in one location.
  2. It increases encapsulation.
  3. It is able to cause extra readable and maintainable code.
  4. if you need to create a class that's for use best by using the enclosing elegance, then it isn't always important to create a separate record for that. as a substitute, you can upload it as "inner class"

  • Static Nested Class:-

If the nested magnificence i.e the magnificence described inside every other elegance, has static modifier carried out in it, then it is referred to a statically nested elegance. because it's far, statically nested training can get entry to simplest static individuals of its outer elegance i.e it can't confer with non-static participants of its enclosing class at once. because of this restriction, statically nested elegance is hardly ever used.

  • Non-Static Nested Class:-

Non-static Nested elegance is the maximum essential form of nested class. it's also known as inner class. It has got admission to all variables and strategies of an Outer class which includes its personal information participants and strategies and can talk over with them at once. but the opposite isn't genuine, this is, Outer magnificence can not without delay get right of entry to individuals of internal magnificence. an inner class may be declared private, public, included, or with default get entry to whereas an Outer elegance will have simplest public or default get admission to.

One more essential factor to observe about an inner class is that it can be created best in the scope of the Outer class. Java compiler generates mistakes if any code out of doors Outer elegance tries to instantiate the inner class at once.

  • Example of Inner Class

class test
{
      public void display()
     {
            demo d=new demo();
            d.show();
     }

     class demo
     {
                 public void show()
                {
                      System.out.println("Inside demo");
                }  
     }
}

class my
{
      public static void main(String[] args)
     {
      test t=new test();
      t.display();
     }
}
Output:
Inside demo


No comments:

Post a Comment

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...