Object Oriented Programming: Unit I: Introduction to OOP and Java

Static Members

with Example Java Programs

The static members can be static data member or static method. The static members are those members which can be accessed without using object. Following program illustrates the use of static members.

Static Members

The static members can be static data member or static method. The static members are those members which can be accessed without using object. Following program illustrates the use of static members.

• static members

1.     static member

2.     static method

pppppppppppppppppppppp

Java Program [Static Prog.java]

/*************************************************************

Program to introduce the use of the static method and static variables

************************************************************/

class StaticProg

{

static int a = 10;

static void fun(int b)

{

System.out.println("b= "+b);

System.out.println("a = "+a);

}

}

class AnotherClass

{

public static void main(String[] args)

{

System.out.println("a= "+Static Prog.a);

StaticProg.fun(20);

}

}

Output

a= 10

b= 20

a= 10

Program Explanation

In above program, we have declared one static variable a and a static member function fun(). These static members are declared in one class StaticProg. Then we have written one more class in which the main method is defined. This class is named as AnotherClass. From this class the static members of class StaticProg are accessed without using any object. Note that we have simple used a syntax

ClassName.staticMember

Hence by using StaticProg.a and StaticProg.fun we can access static members

Points to remember about static members

•In Java the main is always static method.

•The static methods must can access only static data.

• The static method can call only the static method and can not call a non static method.

• The static method can not refer to this pointer.

• The static method can not refer to super method.

Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : with Example Java Programs - Static Members


Object Oriented Programming: Unit I: Introduction to OOP and Java



Under Subject


Object Oriented Programming

CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation



Related Subjects


Discrete Mathematics

MA3354 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Digital Principles and Computer Organization

CS3351 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Foundation of Data Science

CS3352 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Data Structure

CS3301 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation


Object Oriented Programming

CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation