Java class member

What is instance members? Explain with an example

Instance members are the attributes and methods of a class

They share one copy each per object

Since an object is an instance of a class, memory has been set aside for each object to contain its own copies of instance members.

Instance variables and instance methods are referred by joining object reference variable and the name of the instance variable or name of the method.

Example
student.studId – an instance variable of student object is referred

student.computeAverage() – an instance method of student object will be invoked

What is instance variable? Explain with an example

1. An instance variable is defined in a class scope
2. Each object of the class has a separate copy of the instance variable
3. Instance variables values are unique to each instance of a class
4. If there are 3 copies of a class Product, there are 3 copies of each instance variable defined in the class
5. Instance variables can be accessed directly by the object reference variable

Example
class Car
{
    String make,fuel;
    int year;
    . . . .
}
  // Create object of Car
  Car myCar = new Car();
  myCar.make = “Honda”;
  myCar.model=”Diesel”;
  myCar.year = 2009;
6. The instance variables make, model and year are accessed by the reference variable ‘myCar’

What is instance method? Explain with an example

Instance methods are belong to the object

1. Instance methods are invoked by the reference variables of the object
2. Instance methods are unique to each instance of a class
3. Instance methods can be accessed directly by the object reference variable

Example
class Car
{
    String make, fuel;
    int year;
    void computeServiceCharges()
    {
       . . . .
    }
    void computeSparesAmount()
    {
       . . . .
    }
}
  // Create object of Car
  Car myCar = new Car();
  myCar.computeServiceCharges();
  myCar.computeSparesAmount();
The instance methods computeServiceCharges() and computeSparesAmount() are accessed by the reference variable ‘myCar’.

What is static member? Explain with an example

Static member belongs to class

Each static member will have exactly one copy per class

Static members are accessible by their class name

Example
Integer.parseInt(), Float.parseFloat(), DriverManager.getConnection() etc.
All methods in java.lang.Math class are static methods
Example
Math.sqr(), Math.log(), Math.abs() etc.
Static methods can not use any instance variables
The keyword ‘this’ can not be used in a static method

The following example illustrates static members:
public class AccessStaticMethod
{
    public static void staticMethod()
    {
       System.out.println(“I am in staticMethod()” );
    }
    public static void main(String[] args)
    {
       num1 = 30;
       num2 = 23;
       staticMethod(); // static method can invoke another static method
    }
}

What is static variable? Explain with an example

Static variables are belongs to the class as a whole

The static variables are allocated in the memory when the class is loaded

Static variables are accessible by their class name

Example
Integer.MIN_VALUE, Math.PI etc.
Static variables can not use any instance variables

The keyword ‘this’ can not be used with static variable

The following example illustrates static variables:
import java.util.Date;
class MyClass
{
    static Date date new Date();
    Date date2 new Date();
    public static void main(String[] args)
    {
       //Display static variable
       System.out.println(“ Static variable” );
       System.out.println(MyClass.date);

     //Delay for five seconds
     try
     {
         Thread.currentThread().sleep(5000);
     }
     catch(InterruptedException e){}
    }
}
Java packages
Java packages - What are Java packages? Explain the importance of Java packages., Steps for creating a package in Java, Explain the packages access specifier, i.e. private, protected, public, default...
Java garbage collector
Java garbage collector - Explain Java Garbage collector. Why garbage collection? Brief explanation of Garbage collection algorithms., Explain the importance of finalizers in Java...
Java super keyword
Importance of 'super' keyword in Java - The keyword ‘super’ is used for referring parent class instance...
Post your comment