Class Elements
Class Elements
class Test
{ variables
methods
constructors
blocks(instance,static)
}
memoris in java:
1. heap : instance data
2. stack : all local data
3. method area
4. SCP (String constant pool)
5. Native method stacks
Local var :
1. Declared : inside the method, cons, blocks.
2. Scope : inside the method, cons, blocks.
3. memory allocated when method started
memory destroyed when method completed.
4. Stored in : Stack memory
instance var :
1. Declared : inside the class
2. Scope : inside the class
3. memory allocated when object is created
memory destroyed when object destroyed.
4. Stored in : Heap memory
Access permission :
instance variables & methods
ex:
class Test
{ //instance var [object created : object destroyed]
int num1 = 10;
int num2 = 20;
//static method
public static void main(String[] args) //JVM
{ //static area
Test t = new Test();
System.out.println(t.num1 + t.num2);
Observation :
class A
{ int num = 10;
}
class B
{ void disp()
{ A a = new A();
System.out.println(a.num);
}
}
Assignment-1 :
class Test
{ 2-ins var
void add()
{ print the addition
}
void mul()
{ print the multiplication
}
public static void main(String[] args)
{ call add
call mul
}
}
Assignment-2:
class Demo
{ 2-ins var
}
class Test
{ void add()
{ print the addition of Demo cls var
}
void mul()
{ print the multiplication Demo cls var
}
public static void main(String[] args)
{ call add
call mul
}
}
Java Methods
2. static methods
static void mul()
{ //body : static area
}
//static variables:
1. Decalred : inside the class with static modifier
2. Scope : inside the class
3. memory allocated when class loading
memory destroyed when class unloading
4. Stored in : method area
class Test
{ //static var [class loading --- class unloading]
static int a = 10;
static int b = 20;
//static method
public static void main(String[] args)
{ //static area
System.out.println(Test.a+Test.b);
Note :
Always access the instance data using object name.
Always access the static data using class-name.
ex: JVM will assign default values only for instance, static but not for local
data.
in java we must assign the values to local variable.
class Test
{ int a; //ins var
static boolean b; //static var
public static void main(String[] args)
{
double d; //local var : error: variable d might not have been
initialized
System.out.println(d);
System.out.println(Test.b);
}
}
ex: final var are fixed constants modifications are not allowed.
class Test
{ public static void main(String[] args)
{ final int a=10;
a = a + 10; //error: cannot assign a value to final variable a
System.out.println(a);
}
}
Note : Local var only one modifier is applicable that is final.
method syntax:
modfiers_list return_type method-name(params_list) throws Exception
public void add(int a,int b)
public int DL(id1,id2,id3) throws
Exception
method signature:
method-name(params_list)
add(int a,int b)
DL(id1,id2,id3)
Test.add(10,20);
t.disp(10,'a',10.5);
Test.login("naresh","anu");
}
}
ex 2: Assignment
class Services
{ static method : voteCheck(name,age,area,aadhar)
age> 18 : eligible for voting your voter
id is : age+aadher : 20+1234 : 201234
<18 not eligible
instance method : mul(int num1,int num2) :
just print the multiplication
ex-3:
case 1: inside the cls more than one method with same signature is not allowed.
class Test
{ void m1() {
}
void m1() {
}
}
error: method m1() is already defined in class Test
case 2:
class Test
{ m1()
{
}
}
error: invalid method declaration; return type required
class Test
{ int add(int a,int b)
{ int c;
c = a+b;
return c;
}
static char disp()
{ System.out.println("Good Morning....");
return 'c';
}
double totalBill(String pname,int pcost,int pquantity)
{ double res;
res = pcost*pquantity;
return res;
}
static String login(String username,String password)
{ if (username=="ratan" && password=="anu")
{ return "Success";
}
else
{ return "Fail";
}
}
public static void main(String[] args)
{ Test t = new Test();
int x = t.add(10,20);
System.out.println("Return Value..."+x);
char y = Test.disp();
System.out.println("Return Value..."+y);
ex-2:
class Test
{ static int add(int a,int b)
{ int c;
c = a+b;
return c;
}
public static void main(String[] args)
{ //Holding the return value & printing
int x = Test.add(10,20);
System.out.println("Return Value..."+x);
ex-3 :
case 1: local var & instance var having diff names.
class Test
{ int i=10,j=20; //instance var
void add(int a,int b) //local var
{ System.out.println(a+b);
System.out.println(i+j);
}
public static void main(String[] args)
{
Test t = new Test();
t.add(100,200);
}
}
case 2: local var & instance var having same names. In this case to represent
instance var use object name.
in place of object it is possible to use 'this' keyword.
class Test
{ int i=10,j=20;
void add(int i,int j)
{ System.out.println(i+j);
System.out.println(this.i+this.j);
}
public static void main(String[] args)
{
Test t = new Test();
t.add(100,200);
}
}
ex-4:
case 1: if the application contains local & instance data then returns local data.
class Test
{ int a=10; //ins var
int m1(int a) //local var
{ return a;
}
public static void main(String[] args)
{ Test t = new Test();
int a = t.m1(100);
System.out.println("Return value...."+a);
}
}
class Test
{ int a=10; //ins var
int m1(int a) //local var
{ return this.a;
}
public static void main(String[] args)
{ Test t = new Test();
int a = t.m1(100);
System.out.println("Return value...."+a);
}
}
Assignment:
class Test
{ 2-ins var
static void add()
{ print the add
}
static void mul()
{ print the mul
}
public static void main(String[] args)
{ call add()
call mul()
}
}
class Test
{ var
methods //logics
}
3. based on single class possible to create the multiple objects every object
required memory.
class Emp
{
}
0-arg cons:
Test()
{ logics.
}
1-arg cons:
Test(int a)
{ logics
}
Constructor calling:
case 1: To call the constructor use this keyword.
class Test
{ Test()
{ this(10); //calling 1-arg cons
System.out.println("0-arg cons");
}
Test(int a)
{ this(10,20);
System.out.println("1-arg cons");
}
Test(int a,int b)
{ System.out.println("2-arg cons");
}
public static void main(String[] args)
{ new Test();
}
}
case 2:
Test()
{ System.out.println("0-arg cons");
this(10); //calling 1-arg cons
}
error: call to this must be first statement in constructor
case 3:
Test()
{ this(10); //calling 1-arg cons
this(10,20);
System.out.println("0-arg cons");
}
error: call to this must be first statement in constructor
ex-1:
class Emp
{ //instance var
int eid;
String ename,company,addr;
double esal;
//constructor used to initialize the data during object creation.
Emp(int eid,String ename,double esal,String company,String addr) //local var
{ //conversion of local to instance
this.eid = eid;
this.ename = ename;
this.esal = esal;
this.company = company;
this.addr = addr;
}
//Methods are used to write the business logics of the application.
void status()
{ if (esal>10000)
{ System.out.println("Good Employee....");
}
else
{ System.out.println("very Good Employee...");
}
}
void disp()
{ System.out.println("Emp id : "+eid);
System.out.println("Emp name : "+ename);
System.out.println("Emp sal : "+esal);
System.out.println("Emp Company : "+company);
System.out.println("Emp Address : "+addr);
}
public static void main(String[] args)
{ Emp e1 = new Emp(111,"ratan",10000.45,"tcs","hyderabad");
e1.disp();
e1.status();
System.out.println("************");
Note: in instance variables for every object separate memory created. here company,
address is duplicated for all objects. to overcome the above problem use static var
ex-2: static variable irrespective of object creation for all objects single copy
created.
class Emp
{ //instance var
int eid;
String ename;
public static final String company="tcs",addr="hyderabad";
double esal;
//constructor used to initialize the data during object creation.
Emp(int eid,String ename,double esal) //local var
{ //conversion of local to instance
this.eid = eid;
this.ename = ename;
this.esal = esal;
}
//Methods are used to write the business logics of the application.
void status()
{ if (esal>10000)
{ System.out.println("Good Employee....");
}
else
{ System.out.println("very Good Employee...");
}
}
void disp()
{ System.out.println("Emp id : "+eid);
System.out.println("Emp name : "+ename);
System.out.println("Emp sal : "+esal);
System.out.println("Emp Company : "+Emp.company);
System.out.println("Emp Address : "+Emp.addr);
}
public static void main(String[] args)
{ Emp e1 = new Emp(111,"ratan",10000.45);
e1.disp();
e1.status();
System.out.println("************");
Note :
i. Isntance data is specific to object where as static data is common
for all objects.
ii. when we are doing modifications on instance variable only one
object is reflected.
when we are doing modifications on static variable then multiple
objects are reflected.