3.class Object Variable Method Constructor
3.class Object Variable Method Constructor
Class:-
In java class is a blue print or template or logical entity of an
object.
Example:-
Object:-
Object is physical entity which means it is physically exists, they
have memory.
Identity:-In java for every object JVM will create unique id known
as hashcode.
Example:-
class Test//class Name
{
int x=10;//state(instance variable)
public static void main(String[]args)//Behaviour(method)
{
Test t=new Test();//object creation
System.out.println(t.x);
}
}
Result:-10
Java Variable:-
variable means which hold some memory.
All variables must be some types its can be primitive types, array
types, class types, interface types etc.
Example:-
public final int id=101;
Division-1
Based on types of value represented by a variable, all variables are
divided into two types.
A. Primitive variable.
B. Reference variable
cust
Division-2
Based on position of declaration and behaviour, all variables are
divided into three parts.
1. instance variable
2. static variable
3. local variable
Instance Variable:-
If the value of a variable is varied from object to object such
types of variables we declared as instance variable.
Static Area:-The area which with static keyword such types of area
known as static area.
Example:-
class Test
{
//instance variable
int a=20;
int b=30;
//static method
public static void main(String[]args)
{
//static area
Test t=new Test();
System.out.println("static area value of a="+t.a);
System.out.println("static area value of a="+t.b);
t.m1();
}
//instance method
void m1()
{
//instance area
System.out.println("instance area value of a="+a);
System.out.println("instance area value of a="+b);
}
}
Result:-
static area value of a=20
static area value of a=30
instance area value of a=20
instance area value of a=30
Static variable:-
Static variable will be declared inside the class but outside of the
method or block or constructor with static modifier.
Exercise:-
class Test
{
//static variable
static int x=10;
//static method
public static void main(String[]args)
{
//static Area
Test t=new Test();
System.out.println(t.x);
System.out.println(Test.x);
System.out.println(x);
/*all are valid but Test.x is highly recommended to call static
variable because its reflect the properties of static variable.*/
}
}
Result:-
10
10
10
We can access static variable from both instance and static area
directly by using its name or with class name.
Exercise:-
class Test
{
//static variable
static int x=10;
static int y=20;
//static method
public static void main(String[]args)
{
//static area
System.out.println(x);
System.out.println(y);
Test t=new Test();
t.m1();
}
//instance method
void m1()
{
//instance area
System.out.println(x);
System.out.println(y);
}
}
Result:-
10
20
10
20
Local variable:-
Sometime temporary or locally requirement of the variable. such
types of variables we declared as local variable.
Local variable means the variable which access with in block where
we declared it, if we try to access that variable from outside of
the block then we will get compile time error.
Example:-
class Test
{
public static void main(String[]args)
{
int a=10;//local variable
int b=20; //local variable
System.out.println(a);
System.out.println(b);
}
}
Result:-
10
20
Example:-
class Test
{
void m1()
{
int x=10;
System.out.println(x);
}
void m2()
{
System.out.println(x);
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
t.m2();
}
}
Result:-
Test.java:10: error: cannot find symbol
System.out.println(x);
symbol: variable x
location: class Test
Exercise:-case-1
class Test
{
public static void main(String[]args)
{
//Local area of main method
int x;//local variable
System.out.println("Hello");
}
}
Result:-Hello
Exercise:-case-2
class Test
{
public static void main(String[]args)
{
//Local area of main method
int x;
System.out.println(x);
}
}
Result:-
Test.java:6: error: variable x might not have been initialized
System.out.println(x);.
METHOD
Method:-
Inside a java class we can’t write any business logic directly.
Inside a class we declare a method and inside that method we write
business logic of programming requirement.
Example:-
class Test
{
int x=10;
int y=20;
System.out.println(x+y);//business logic
}
To Solve this problem, we write all business logic inside the method
class Test
{
int x=10;
int y=20;
public static void main(String[]args)//static method
{
Test t=new Test();
System.out.println(t.x+t.y);//business logic
}
}
Method Syntax:-
[Modifier-List] Return-Type Method-Name(parameter List)throws
Exception
Method-Name--->functionality name()----------------->[mandatory]
Parameter-List--->input to functionality------------>[optional]
Example:-
Modifier
Return-type
public void m1(int x)
Method name
Parameter
m1(int a)
Method name are same but method signature
are different
m1(int a,int b)
Instance Method:-
The method which declared without static keyword such type of method
known as instance method.
Static method:-
The method which declared with static keyword such types of method
considered as static method.
we can access static method inside the instance area or static area
by using method name directly but this rule is applicable only for
within the class but outside of the class we access by using class
name.
Result:-
m1()static method
m1()static method
Note:-if we are not commenting line 12 then we will get compile time
error saying
Case1:-
Example:-
class Test
{
public void m1()
{
System.out.println("m1()instance method");
}
public static void m2()
{
System.out.println("m2() static method");
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
Test.m2();
}
}
Result:-
m1()instance method
m2() static method
case2:-
Instance method and static method with parameter
Case3:-
While calling method we can pass variable as argument also.
Example:-
class Test
{
void m1(int a,boolean b,char ch)
{
System.out.println(a);
System.out.println(b);
System.out.println(ch);
}
public static void main(String[]args)
{
int a=10;
boolean b=true;
char ch='v';
Test t=new Test();
t.m1(a,b,ch);
}
}
Result:-
10
true
v
t.m2(10,r);
Test.m2(10,new Rat());
}
}
Result:-
m1()instance method
m1()instance method
m2() static method
m2() static method
Example:-
class Test
{
void m1(int a)
{
System.out.println("m1 instance method");
}
static void m1(int b)
{
System.out.println("m1 static method");
}
public static void main(String[]args)
{
Test t=new Test();
t.m1(10);
}
}
Result:-
Test.java:7: error: method m1(int) is already defined in class Test
static void m1(int b)
Example:-
class Test
{
public void m1()
{
void m2()
{
System.out.println("m2()inner method");
}
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
}
}
Result:-
Test.java:5: error: illegal start of expression void m2()
Test.java:5: error: ';'
Expected void m2()
Case7:-
Type-casting at method argument level
Example:-
class Test
{
public void m1(int x)
{
System.out.println("int value="+x);
}
public void m1(byte b)
{
System.out.println("byte value="+b);
}
public void m1(short s)
{
System.out.println("short value="+s);
}
public void m1(char ch)
{
System.out.println("char value="+ch);
}
public void m1(long l)
{
System.out.println("long value="+l);
}
public void m1(float f)
{
System.out.println("float value="+f);
}
public void m1(double d)
{
System.out.println("double value="+d);
}
public static void main(String[]args)
{
Test t=new Test();
t.m1(10);
t.m1((byte)20);
t.m1((short)30);
t.m1(40l);
t.m1('V');
t.m1(50.50f);
t.m1(60.60);
}
}
Result:-
int value=10
byte value=20
short value=30
long value=40
char value=V
float value=50.5
double value=60.6
Case8:-
A method can call any number of method but once method execution is
completed the control returns to the caller method.
Example:-
class Test
{
public void m1()
{
m2();
System.out.println("m1()mehtod");
m2();
}
public void m2()
{
m3(100);
System.out.println("m2()method");
m3(100);
}
public void m3(int x)
{
System.out.println("m3()method");
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
}
}
Result:-
m3()method
m2()method
m3()method
m1()mehtod
m3()method
m2()method
m3()method
Example:-
class Test
{
public m1()
{
System.out.println("m1()method not contain return type");
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
}
}
Result:-
If we declare a method with return type, other than void then method
body must return return-type value with return keyword otherwise, we
will get compile time error.
Result:-vikas
Case2:-A method declare with return type other then void and method
body not contain any return statement then we will get compile time
error.
Example:-
class Test
{
float m1()
{
System.out.println("vikas");
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
}
}
Result:- Test.java:6: error: missing return statement
Every method is able to return the value and holding that return
value is optional, but it is highly recommended to hold the return
value to check the status of the method.
Example:-
class Test
{
public boolean m1(int x,char ch)
{
System.out.println("m2() method");
System.out.println(x+".."+ch);
return true;
}
public static void main(String[]args)
{
Result:-
m2() method
10..v
m1()method return values=true
Example:-
class X
{}
class Emp
{}
class Test
{
public X m1()
{
System.out.println("m1()method");
X x=new X();
return x;
}
public Emp m2()
{
System.out.println("m2()method");
Emp e=new Emp();
return e;
}
public static String m3()
{
System.out.println("m3()method");
return "vikas";
}
public static void main(String[]args)
{
Test t=new Test();
X x1=t.m1();
System.out.println(x1);
Emp e=t.m2();
System.out.println(e);
String s=Test.m3();
System.out.println(s);
}
}
Result:-
m1()method
X@15db9742
m2()method
Emp@6d06d69c
m3()method
vikas
Template Method:-
In general, after complete the task we required to call all methods,
so at the time of method calling we should remember two things.
1. Number of method and name of method.
2. Order of method(which one is first and which one is later).
Example:-
//Template method
class Test
{
void customer()
{
System.out.println("customer");
}
void product()
{
System.out.println("product");
}
void selection()
{
System.out.println("selection");
}
void billing()
{
System.out.println("billing");
}
void deliveryManager()
{
customer();
product();
selection();
billing();
}
public static void main(String[]args)
{
Test t=new Test();
t.deliveryManager();
}
}
Result:-
customer
product
selection
billing
Example:-
class Test
{
public void m1()
{
System.out.println("m1() method");
m1();
}
public static void main(String[]args)
{
Test t=new Test();
t.m1();
}
}
Constructors:-
Whenever we are creating an object some piece of the code will be
executed automatically to perform initialization state of an object
this piece of the code is known as constructor.
Example:-
class Test
{
Test()//constructor name and class name both are same.
{
27 CLASS OBJECT VARIABLE METHOD CONSTRUCTOR
28 | P a g e
System.out.println("constructor invoked");
}
public static void main(String[]args)
{
Test t=new Test();
}
}
Default constructor:-
If our class doesn’t contain any constructor then java
compiler provides default constructor.
Inside the class default constructor is invisible mode.
It is always no parameter constructor.
Non-Parameterized Constructor:-
The user defined constructor which are not contain any parameter
such types of constructor are called Non-parameterized constructor.
Example:-
class Test
{
int x;
int y;
Test()
{
x=10;
y=20;
}
public void m1()
{
System.out.println(x+".."+y);
}
public static void main(String[]args)
{
Test t1=new Test();
Test t2=new Test();
t1.m1();
t2.m1();
}
}
Result:-
10..20
10..20
Parameterized Constructor:-
The user defined constructor which are contain at least one
parameter such types of constructors are called parameterized
constructor.
Example:-
class Test
{
Test(int a)
{
System.out.println("1-arg parameterized constructor");
}
Test(int a,int b)
{
Result:-
1-arg parameterized constructor
2-arg parameterized constructor
Result:-
10..20
10..20
Example:-
class Test
{
Test()
{
System.out.println("No-arg constructor");
}
Test(int a)
{
System.out.println("int arg constructor");
}
Test(double d)
{
System.out.println("double arg constructor");
}
public static void main(String[]args)
{
Test t1=new Test();
Test t2=new Test(10);
Test t3=new Test(12.22);
Test t4=new Test(10l);
}
}
Result:-
No-arg constructor
int arg constructor
double arg constructor
double arg constructor
Example:-
class Parent
{
Parent()
{
System.out.println("Hello parent");
}
}
class Child extends Parent
{
Child(int a)
{
super();
System.out.println("Hello Child");
}
public static void main(String[]args)
{
//Child c1=new Child();
Child c2=new Child(10);
}
}
Result:-
Hello parent
Hello Child
Case Study:-
Case1:-
Inside a class if we declare at least one constructor it may be
parameterized or non-parameterized then java compile won’t create
default constructor.
Example:-
class Test
{
Test(int a)
{
System.out.println("1-arg user defined constructor");
}
Test(int a,int b)
{
System.out.println("2-arg user defined constructor");
}
public static void main(String[]args)
{
Test t1=new Test();
Test t2=new Test(10);
Test t3=new Test(10,20);
}
}
Result: -In the above application we will get compile time error
because java compiler is unable to create default constructor, java
compiler is created default constructor if and only if there is no
any user defined constructor is available.
Example:-
class Test
{
int x;
int y;
Test(int x,int y)
{
this.x=x;
this.y=y;
}
void m1(int x,int y)
{
this.x=x;
this.y=y;
}
void info()
{
System.out.println(x+".."+y);
}
public static void main(String[]args)
{
Test t=new Test(10,20);
t.m1(100,200);
t.info();
}
}
Result:- 100..200
Case3:-
Inside a constructor if we are using super() or this() statement
then this statement should be the first statement otherwise we will
get compile time error.
Example:-
class Test
{
Test()
{
System.out.println("Hello");
super();
}
}
Result:- error: call to super must be first statement in constructor
super();
Example:-
class Test
{
Test()
{
super();
this();
}
}
Result:-
error: call to this must be first statement in constructor
this();
Case5:-We can use super() and this() statement only inside the
constructor no any other place otherwise we will get compile time
error.
Example:-
class Test
{
public static void main(String[]args)
{
super();
System.out.println("Hello");
}
}
Result:-
error: call to super must be first statement in construct or
super();
Case6:-
A constructor can call another constructor by using this() .
class Test
{
Test()
{
this(10);
System.out.println("0-arg Constructor");
}
Test(int x)
{
this(10,20);
System.out.println("1-arg Constructor");
}
Test(int x,int y)
{
System.out.println("2-arg Constructor");
}
public static void main(String[]args)
{
Test t=new Test();
}
}
Result:-
2-arg Constructor
1-arg Constructor
0-arg Constructor
Case7:-
Recursion constructor:- constructor call each other is known as
construction recursion, In our program there may be chance recursion
constructor call which create compile time error.
Example:-
class Test
{
Test()
{
this(20);
System.out.println("0-arg constructor");
}
Test(int a)
{
this();
System.out.println("1-arg constructor");
}
public static void main(String[]args)
{
Test t=new Test();
}
}
Result:-
java:8: error: recursive constructor invocation Test(int a)
Case7:-
Note:-If parent class method contains any argument constructor then
in child class we should provide special care with respect to
constructor.
Example:-
Case8:-
If Parent class constructor throw any checked exception then child
class constructor should be throws same types of exception or it
Parent types otherwise we will get compile time error.
Example:-
import java.io.*;
class Parent
{
Parent() throws IOException
{
}
}
class Child extends Parent
{
Child()
{
}
}
We can't create object for abstract class but abstract class can
contain constructor what is the need ?
Abstract class constructor will be executed for every child class
object creation to perform initialization of child class object
only.
Example:-
abstract class Parent
{
Parent()
{
System.out.println(this.hashCode());
//11394033//here this means child class object
}
}
class Child extends Parent
{
Child()
{
System.out.println(this.hashCode());//11394033
}
}
class Test
{
public static void main(String[] args)
{
Child c=new Child();
System.out.println(c.hashCode());//11394033
}
}