Java Class Members

Java Class Members

Java Class Members, Create Java Static Variables, Java Static Methods, Create Java Non static Variables, Non static Methods, and call Java Variables and Java Methods.

Create Java User defined methods with arguments, without arguments, with return a value, and without return any value.

Create Java Static Variables, Static Methods, and Call

Create Object using Java User defined class.

Create Java Non-static Variables, Non-static Methods, and Call by invoking Object

Java Tutorial

Java Step by Step Tutorial

Grand Example for Creating and Calling Java Static and Non Static Class Members

public class Class1 {
//Create Static Variables
static int x=100, y=200;
//Create Non static Variables
int a=123, b=456;

//Create a Static method with return a Value
public static int add() {
int num1=10, num2=20;
int res=num1+num2;
return res;
}
//Create a Static method without returning any Value
public static void sub() {
int num1=100, num2=200;
int res=num1-num2;
System.out.println(res);
}
//Create a Non static method with return a Value
public int multiply() {
int x=123, y=30;
int res=x*y;
return res;
}

//Create a Non static method without returning any Value
public void comparison() {
int a=100, b=90;

if (a>b) {
System.out.println(“A is Big Number”);
}
else {
System.out.println(“B is Big Number”);
}
}
public static void main(String[] args) {
//Create Object
Class1 obj = new Class1();

//Call Static Class Members (Variables)
System.out.println(x+y);//300

//Call Non Static Class Members (Variables)
System.out.println(obj.a+obj.b);//579

System.out.println(“”);

//Call Static Class Members (Methods)
int res= add();
System.out.println(res);//30

System.out.println(add());//30

sub();//-100
System.out.println(“”);

//Call Non static Class Members (Methods)

int res2= obj.multiply();
System.out.println(res2);

obj.comparison();//A is Big Number
}
}


Calling Java Static and Non Static Class Members from an another Class

public class Class2 {

public static void main(String[] args) {
Class1 abc = new Class1();
//Call Static Class Members (Variables)
System.out.println(Class1.x+Class1.y);//300

//Call Non Static Class Members (Variables)
System.out.println(abc.a+abc.b);//579

System.out.println(“”);

//Call Static Class Members (Methods)
int res= Class1.add();
System.out.println(res);//30

System.out.println(Class1.add());//30

Class1.sub();//-100
System.out.println(“”);

//Call Non static Class Members (Methods)

int res2= abc.multiply();
System.out.println(res2);

abc.comparison();//A is Big Number
}
}

G C Reddy

G C Reddy, Working as a Software Testing Trainer in Hyderabad, India.

You may also like...