0% found this document useful (0 votes)
11 views13 pages

Class Elements

The document provides an overview of Java class elements, memory management, variable types, methods, and constructors. It explains the differences between instance variables, local variables, and static variables, along with their scopes and memory allocation. Additionally, it covers method definitions, examples of method overloading, and the rules for constructor creation and calling in Java.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Class Elements

The document provides an overview of Java class elements, memory management, variable types, methods, and constructors. It explains the differences between instance variables, local variables, and static variables, along with their scopes and memory allocation. Additionally, it covers method definitions, examples of method overloading, and the rules for constructor creation and calling in Java.

Uploaded by

kiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

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

variables : used to store the data


every variable contains some type.
1. local var
2. instance var
3. static var

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

Direct Access Acccess using object

instance area static area

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);

t.add(); // calling add() method


}
//instance method
void add()
{ //instance area
System.out.println(num1 + num2);
}
}

Observation :
class A
{ int num = 10;
}
class B
{ void disp()
{ A a = new A();
System.out.println(a.num);
}
}

Note : in java always access the instance data using object-name


instance to instance direct access is possible but only with in the
same class.

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

metods are used to write the logics of the application.


class Test
{ int a=10;
int b=20;
void add(){
System.out.println(a+b);
}
}

There are two types of methods in java


1. instance method
void add()
{ //body : instance area
}

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);

Test t = new Test();


t.add();
}
//instance method
void add()
{ //isntance 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.

Methods: used to write the logics of the application.

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);

Test t = new Test();


System.out.println(t.a);

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)

ex-1: methods with arguments


class Test
{ void wish()
{ System.out.println("Good Morning....");
}
static void add(int a,int b)
{ System.out.println(a+b);
}
void disp(int a,char ch,double d)
{ System.out.println(a+" "+ch+" "+d);
}
static void login(String username,String password)
{ if (username=="ratan" && password=="anu")
{ System.out.println("Login Success...."+username);
}
else
{ System.out.println("Login fail...."+password);
}
}
public static void main(String[] args)
{ Test t = new Test();
t.wish();

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

static method : register(username,password,age,addr,email,mobile)


addr=="hyd" && age>20 : your are eligible
for mrg u r reg id: mob+age : 8989898+25 : 898989825
instance method : wish(String name)
Good morning name
}
class TestClient
{ public static void main(String[] args)
{ call 4 methods by passing the arguments.
}
}

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

case 3: java supports inner classes but not inner methods.


class Test
{ void m1()
{ void m2() // inner methods : error: illegal start of expression
{
}
}
}

Method formats are two types


1. method without return value : void
2. Method with return value

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);

double bill = t.totalBill("pen",20,5);


System.out.println("Total bill..."+bill);

String status = Test.login("ratan","anu");


System.out.println("Login Status..."+status);
}
}

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);

//Directly printing the return value


System.out.println(Test.add(10,20));
}
}

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.

Note: this keyword represent the current class object.


Inside the static area, this keyword is not allowed.

class Test
{ int i=10,j=20;
void add(int i,int j)
{ System.out.println(i+j);

Test t = new Test();


System.out.println(t.i+t.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);
}
}

case 2: in below example returns instance data.


class Test
{ int a=10; //ins var
int m1()
{ return a;
}
public static void main(String[] args)
{ Test t = new Test();
int a = t.m1();
System.out.println("Return value...."+a);
}
}
case 3: if the application contains local & instance data then returns local data.
but to return the instance data use this keyword.

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 vs. Object :


1. class is a logical entity contains the logic of the application.
object is physical entity represent memory

class Test
{ var
methods //logics
}

Test t = new Test(); //memory

2. class is a blueprint it decides object creation without class not possible to


create the object.

3. based on single class possible to create the multiple objects every object
required memory.
class Emp
{
}

Emp e1 = new Emp();


Emp e2 = new Emp();
4. declare the class using class keyword & create the object using the new keyword.

Object creation line :


Test t = new Test();
class-name object-name = keyword constructor;

Rules to declare the cons:


a. cons name & class-name must be same.
b. cons can take arguments.
c. cons return type is not allowed.

0-arg cons:
Test()
{ logics.
}

1-arg cons:
Test(int a)
{ logics
}

There are two types of constructors:


1. default cons : compiler : 0-arg cons with empty impl
class Test
{ void wish()
{ System.out.println("Good Morning");
}
/* default constructor
Test()
{ empty impl
}
*/
public static void main(String[] args)
{ Test t = new Test();
t.wish();
}
}

2. user defined cons : developer :


class Test
{ void wish()
{ System.out.println("Good Morning");
}
Test()
{ System.out.println("0-arg cons");
}
Test(int a)
{ System.out.println("1-arg cons");
}
public static void main(String[] args)
{
Test t1 = new Test();
Test t2 = new Test(10);
t1.wish();
t2.wish();
}
}
Note:
i. constructors is a special method used to write the logics these logics are
executed when we create the object.
ii. user defined 0-arg cons is not a default cons. only compiler genertaed cons is
called default cons.
iii. inside the class if we are not declaring any cons(0-arg, params cons) then
only compiler generates default cons.

Two formats of object creation


1. named object
2. name less object
class Test
{ void wish()
{ System.out.println("Good Morning");
}
public static void main(String[] args)
{ //named object : an object with name
Test t = new Test();
t.wish();
//name less object : An object without name
new Test().wish();
}
}

Observation : method calling : Named object recommanded.


void m1(){}
void m2(){}
void m3(){}

named object : one object is created calling three methods


Test t = new Test();
t.m1();
t.m2();
t.m3();

name less object : three objects are created.


new Test().m1();
new Test().m2();
new Test().m3();

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

Note: one method can call multiple methods.


one constructor can call only one 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("************");

Emp e2 = new Emp(222,"anu",7000.45,"tcs","hyderabad");


e2.disp();
e2.status();
}
}

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("************");

Emp e2 = new Emp(222,"anu",7000.45);


e2.disp();
e2.status();
}
}

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.

ex: Assignment : this application only "TS"


class Electricity
{
during object creation initialize the data :
houseno,name,city,units,state,month

take the method : calculateBill() :


<100 : 1-unit : 2rs 200
100 & 150 : 1-unit : 3rs 150
150 & 200 : 1-unit : 4rs 200
>200 : 1- unit : 5rs 100

Take the disp() : inside the method print the data

public static void main(String[] args)


{ create the object pass the data.
call the disp() method.
call the calculateBill() method.
}
}

number of units : 123


200 + 69 = 269
number of units : 220
200 + 150 + 200 + 100 = 650

You might also like