Day 4

DAY 4
JAVA
CONSTRUCTORS
constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects to desired values or default values at the time of object creation.
Everytime an object is created using new() keyword, atleast one constructor is called. It is called a default constructor.
It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't have any.

Rules for creating java constructor
There are basically two rules defined for the constructor.
  1. Constructor name must be same as its class name
  2. Constructor must have no explicit return type

Types of java constructors

There are two types of constructors in java:

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
Default constructor:- A constructor is called "Default Constructor" when it doesn't have any parameter.
Parameterized constructor:- A constructor which has a specific number of parameters is called parameterized constructor.

CONSTRUCTOR OVERLOADING

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler by the number of parameters in the list and their types.


class Student5{
    int id;
    String name;
    int age;
    Student5(int i,String n){
    id = i;
    name = n;
    }
    Student5(int i,String n,int a){
    id = i;
    name = n;
    age=a;
    }
    void display(){System.out.println(id+" "+name+" "+age);}

    public static void main(String args[]){
    Student5 s1 = new Student5(111,"Karan");
    Student5 s2 = new Student5(222,"Aryan",25);
    s1.display();
    s2.display();
   }
}

Image result for difference between methods and constructors in java

This KEYWORD
Keyword 'THIS' in Java is a reference variable that refers to the current object. It can be used to refer current class instance variable. It can be used to invoke or initiate current class constructor.

class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
this.rollno=rollno;  
this.name=name;  
this.fee=fee;  
}

STATIC KEYWORD
The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a keyword that are used for share the same variable or method of a given class. This is used for a constant variable or a method that is the same for every instance of a class. The main method of a class is generally labeled static.
No object needs to be created to use static variable or call static methods, just put the class name before the static variable or method to use them. Static method can not call non-static method.

In java language static keyword can be used for following
  • variable (also known as class variable)
  • method (also known as class method)
  • block
  • nested class

Static variable
If any variable we declared as static is known as static variable.
  • Static variable is used for fulfill the common requirement. For Example company name of employees,college name of students etc. Name of the college is common for all students.
  • The static variable allocate memory only once in class area at the time of class loading.

Advantage of static variable
Using static variable we make our program memory efficient (i.e it saves memory).
When and why we use static variable
Suppose we want to store record of all employee of any company, in this case employee id is unique for every employee but company name is common for all. When we create a static variable as a company name then only once memory is allocated otherwise it allocate a memory space each time for every employee.
INNER CLASS
Nested Classes. In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. Theclass written within is called the nested class, and the class that holds the inner class is called the outerclass.
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.
Following is the program to create an inner class and access it. In the given example, we make the inner class private and access the class through a method.
Example

class Outer_Demo {
   int num;
   
   // inner class
   private class Inner_Demo {
      public void print() {
         System.out.println("This is an inner class");
      }
   }
   
   // Accessing he inner class from the method within
   void display_Inner() {
      Inner_Demo inner = new Inner_Demo();
      inner.print();
   }
}
   
public class My_class {

   public static void main(String args[]) {
      // Instantiating the outer class 
      Outer_Demo outer = new Outer_Demo();
      
      // Accessing the display_Inner() method.
      outer.display_Inner();
   }
}

Here you can observe that Outer_Demo is the outer class, Inner_Demo is the inner class, display_Inner() is the method inside which we are instantiating the inner class, and this method is invoked from the main method.

FINALIZED METHOD

SYNTAX

protected void finalized()
{
}

Purpose:-  The finalize() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.

Comments

Popular posts from this blog

Day 7

Industrial training in CDAC