Java – Class Members

Class Members:

  • The Members which we can define inside the class.
  • Class Members includes
    • Variables
    • Methods
    • Blocks
    • Constructor

Variables: Variable stores information of class(object).We store information in java using 4 types of variables

  • Static Variables:
    • A variable with static keyword inside class and outside to methods.
    • Static variables store common information of all Objects.
    • Static variables get memory only once.
    • Static variables can access using class-name.
  • Instance Variables:
    • A variable inside class and outside to methods.
    • Instance variables store specific information of all Objects.
    • Instance variables get separate memory for each object.
    • Instance variables can access using Object-Reference.
  • Method Parameters:
    • A variable inside the method definition parenthesis.
    • Parameters are used to store input in a Method.
    • Parameters can access directly but only inside that method.
  • Local Variables:
    • A variable inside the method body.
    • Store processed information inside the Method.
    • Access local variables directly and only inside the method.

Blocks:

  • Block is a set of instructions without identity
  • Block is either static or instance(anonymous)

Static Block: Defining a block using static-keyword. JVM invokes static block when class execution starts.

static
{
            statements;
}

Instance Block: Defining a block without static-keyword. JVM invokes instance block every time when object creates.

{
            statements;
}

Methods:

  • A Block of instructions with an identity.
  • Method takes input, process input and returns output
  • Methods performs operations on data

Static Method: Defining a method using static keyword. We can access static methods using class-name.

static void display()
{
            logic;
}

Instance Method: Defining a method without static keyword. We can access instance methods using object-reference.

void display()
{
            logic;
}

Constructor: Defining a method with class name. Return type is not allowed. We must invoke the constructor in object creation process.

class Account
{
            Account(){
                        statements;
            }
}
Scroll to Top