JAVA PROGRAMS

Pre-Requisites:

Knowledge on Core Java, OOPs Concepts, Conditional and Control Statements,

JAVA PROGRAMS

import java.lang.*;

class Test1

{

public void display()

{

System.out.println("AM IN TEST1 CLASS");

}

}

class Test

{

public static void main(String args[])

{

System.out.println("THIS IS MY FIRST JAVA PROGRAM");

Test1 t1=new Test1();

t1.display();

}

}

// Write a program on introducing classes, constructors overloading and returning a value.

import java.lang.*;

class Box

{

double h,w,d;

Box()

{

h=w=d=-1;

}

Box(double ht,double wd, double dp)

{

h=ht;

w=wd;

d=dp;

}

Box(double len)

{

h=w=d=len;

}

Box(Box b)

{

h=b.h;

w=b.w;

d=b.d;

}

double volume()

{

return h*w*d;

}

}

class BoxDemo

{

public static void main(String args[])

{

System.out.println("box demo");

Box b1=new Box(10,20,30);

double vol;

vol=b1.volume();

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

Box b2=new Box(10);

vol=b2.volume();

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

Box b3=new Box();

vol=b3.volume();

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

Box b4=new Box();

vol=b4.volume();

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

}

}

// write a program on using "this" keyword and passing parameters to methods.

import java.lang.*;

class Student

{

int rollno;

String name;

void setData(int rollno,String name)

{

this.rollno=rollno;

this.name=name;

}

void show()

{

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

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

}

}

class StudentDemo

{

public static void main(String args[])

{

Student s1=new Student();

s1.setData(22,"vimala");

s1.show();

}

}

// write a program to demonstrate method overloading.

import java.lang.*;

class OverLoadDemo

{

void test()

{

System.out.println("No Parameters");

}

void test(int a)

{

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

}

void test(int a,int b)

{

System.out.println("a and b:"+a+""+b);

}

double test(double a)

{

System.out.println("double a:"+a);

return a*a;

}

}

class MethodOverLoad

{

public static void main(String args[])

{

OverLoadDemo ob= new OverLoadDemo();

double result;

ob.test();

ob.test(10);

ob.test(10,20);

result=ob.test(123.25);

System.out.println("Result of ob.test(123.25):"+result);

}

}

// write a program to demonstrate call by value.

import java.lang.*;

class Test2

{

void meth(int i, int j)

{

i*=2;

j/=2;

}

}

class CallByValue

{

public static void main(String args[])

{

Test2 ob=new Test2();

int a=15,b=20;

System.out.println("a and b before call:"+a+" "+b);

ob.meth(a,b);

System.out.println("a and b after call:"+a+" "+b);

}

}

// write a program to demonstrate call by reference.

import java.lang.*;

class Test3

{

int i,j;

Test3(int i, int j)

{

this.i=i;

this.j=j;

}

void meth(Test3 ob)

{

ob.i*=2;

ob.j/=2;

}

}

class CallByRef

{

public static void main(String args[])

{

Test3 ob=new Test3(15,20);

System.out.println("ob.i and ob.j before call:"+ob.i+" "+ob.j);

ob.meth(ob);

System.out.println("ob.i and ob.j after call:"+ob.i+" "+ob.j);

}

}

// write a program to demonstrate Returning Objects.

import java.lang.*;

class Test

{

int a,b;

Test(int x,int y)

{

a=x;

b=y;

}

Test fpincrByTen()

{

Test temp=new Test(a+10,b);

return temp;

}

}

class ReturnObj

{

public static void main(String args[])

{

System.out.println("ReturnObj");

Test t1 =new Test2(2,6);

Test t2;

t2=t1.fpincrByTen();

System.out.println("value of a at t1 is "+t1.a+" "+t1.b);

System.out.println("valuea of a at t2 is "+t2.a+" "+t2.b);

}

}

// write a program to demonstrate Recursion.

import java.lang.*;

class Factorial

{

int fact(int n)

{

int result;

if(n==1) return 1;

result=fact(n-1)*n;

return result;

}

}

class Recursion

{

public static void main(String args[])

{

System.out.println("Recursion");

Factorial f1 =new Factorial();

System.out.println("Factorial of 5 is"+f1.fact(5));

}

}

// write a program to demonstrate how to invoke static variables and static member functions.

import java.lang.*;

class StaticDemo

{

static int a=42;

static int b=99;

static void callme()

{

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

}

}

class StaticByName

{

public static void main(String args[])

{

System.out.println("Static by Name");

StaticDemo.callme();

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

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

}

}

// write a program to demonstrate static variable, methods and blocks.

import java.lang.*;

class UseStatic

{

static int a=3;

static int b;

static void meth(int x)

{

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

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

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

}

static void meth(int x,int y)

{

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

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

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

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

}

Static void meth(double x)

{

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

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

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

}

static

{

System.out.println("static first block initialized");

b=a*4;

a=a+3;

}

static

{

System.out.println("static lastblock initialized");

b=b*2;

a=a-3;

}

public static void main(String args[])

{

System.out.println("usestatic");

meth(42);

meth(32,56);

meth(42.2);

}

}

// write a program to demonstrate NestedInnerClass

import java.lang.*;

class OuterClass

{

void display()

{

System.out.println("Inside OuterClass");

InnerClass i1=new InnerClass();

i1.show();

}

class InnerClass

{

void show()

{

System.out.println("Inside InnerClass");

}

}

}

class NestedInnerDemo

{

public static void main(String args[])

{

OuterClass i1=new OuterClass();

i1.display();

}

}

// write a program to demonstrate charAt() method.

import java.lang.*;

class CharAt

{

public static void main(String args[])

{

System.out.println("CharAt");

char ch;

ch="Vishwa".charAt(1);

System.out.println(ch);

}

}

// write a program to demonstrate equals() methods of String.

import java.lang.*;

class EqualsDemo

{

public static void main(String args[])

{

System.out.println("EqualsDemo");

String s1="Hello";

String s2="Hello";

String s3="hai";

String s4="HELLO";

System.out.println(s1+"equals"+s2+"-->"+s1.equals(s2));

System.out.println(s1+"equals"+s3+"-->"+s1.equals(s3));

System.out.println(s1+"equals"+s4+"-->"+s1.equals(s4));

System.out.println(s1+"equalsIgnoreCase"+s4+"-->"+s1.equalsIgnoreCase(s4));

}

}

// write a program to demonstrate getchar() method.

import java.lang.*;

class GetChars

{

public static void main(String args[])

{

System.out.println("getchars");

String s="This is a demo of the getChars method.";

int start=10;

int end=14;

char buf[]=new char[end-start];

s.getChars(start,end,buf,0);

System.out.println(buf);

}

}

// write a program to make a string from char[]array.

import java.lang.*;

class MakeString

{

public static void main(String args[])

{

System.out.println("MakeString");

char c[]={'j','a','v','a'};

String s1=new String(c);

String s2=new String(s1);

String s3=new String(c,1,3);

System.out.println(s1+","+s2+","+s3);

}

}

// write a program to demonstrate String length().

import java.lang.*;

class StringLength

{

public static void main(String args[])

{

System.out.println("StringLength");

char c[]={'j','a','v','a'};

String s1=new String(c);

System.out.println(s1.length());

}

}

// write a program to demonstrate String Concat.

import java.lang.*;

class StringConcat

{

public static void main(String args[])

{

System.out.println("StringConcat");

String age = "9";

String s = "He is "+age+"years old.";

System.out.println(s);

String longstr= "this is very very"+

much intresting and how both+

lines are concated;

System.out.println(longstr);

int age1 = 9;

String s1="He is "+age+"years old.";

System.out.println(s1);

String s2="four:"+2+2;

System.out.println(s2);

String s3="four:"+(2+2);

System.out.println(s3);

}

}

// write a program on substring.

import java.lang.*;

class StringLiterals

{

public static void main(String args[])

{

System.out.println("StringLiterals!");

char chars[]={'a','b','c'};

String s1 = new String(chars);

String s2 = "abc";

System.out.println(s1.length());

System.out.println(s2.length());

System.out.println("vimala".length());

}

}

// write a program to demonstrate String Replace.

import java.lang.*;

class StringReplace

{

public static void main(String args[])

{

System.out.println("StringReplace");

String org = "This is a test.This is, too.";

String search = "is";

String sub = "was";

String result ="";

int i;

do

{

System.out.println(org);

i=org.indexOf(search);

if(i!=-1)

{

result = org.substring(0,i);

result = result + sub;

result=result+org.substring(i+search.length());

org= result;

}

}while(i!=-1);

}

}

// write a program on Substring.

import java.lang.*;

class SubStringCons

{

public static void main(String args[])

{

System.out.println("SubStringCons!");

byte ascii[]={65,66,67,68,69,70};

String s1=new String(ascii);

System.out.println(s1);

String s2=new String(ascii,2,3);

}

}

// write a program to demonstrate toString() method.

import java.lang.*;

class Box

{

double h,w,d;

Box(double ht,double wd,double dp)

{

h=ht;

w=wd;

d=dp;

}

public String toString()

{

return "dimensions are"+w+"by"+d+"by"+h+".";

}

}

class toStringDemo

{

public static void main(String args[])

{

System.out.println("toStringDemo");

Box b=new Box(10,12,14);

String s ="Boxb:"+b;

System.out.println(b);

System.out.println(s);

}

}

// write a program to demonstrate cmd-line-arguments

import java.lang.*;

class CmdLineArgs

{

pulblic static void main(String args[])

{

for(int i=0;i<args.length;i++)

System.out.println("args["+i+"]="+args[i]);

}

}

// write a program on the usage of super keyword.

import java.lang.*;

class A

{

int i;

}

class B extends A

{

int i;

B(int a, int b);

{

super.i=a;

i=b;

}

void show()

{

System.out.println("i value in A Class is"+super.i);

System.out.println("i value in this B Class is"+i);

}

}

class UseSuper

{

public static void main(String args[])

{

System.out.println("UseSuper");

B suboji = new B(1,2);

suboji.show();

}

}

// write a program to demonstrate DynamicMethodDispatch.

import java.lang.*;

class EClass

{

void callme()

{

System.out.println("Inside EClass callme()");

}

}

class IClass ectends EClass

{

void callme()

{

System.out.println("Inside IClass callme()");

}

}

class DClass extends IClass

{

void callme()

{

System.out.println("Inside Dclass callme()");

}

}

class Dispatch

{

public static void main(String args[])

{

System.out.println("Dispatch!");

EClass e= new EClass();

IClass i = new IClass();

DClass d = new DClass();

EClass r;

r=e;

r.callme();

r=i;

r.callme();

r=d;

r.callme();

}

}

// write a program to demonstrate Abstract Class

import java.lang.*;

{

double d1,d2;

Figure(double a,double b)

{

d1=a;

d2=b;

}

abstract double area();

}

class Triangle extends Figure

{

Triangle(double a,double b)

{

super(a,b);

}

double area()

{

return d1*d2/2;

}

}

class Rectangle extends Figure

{

Rectangle(double a,double b)

{

super(a,b);

}

double area()

{

return d1*d2;

}

}

class AbstractAreas

{

public static void main(String argsp[])

{

System.out.println("AbstractAreas");

Rectangle r = new Rectangle(9,5);

Rectangle r1 = new Rectangle(9,5);

Triangle r1 = new Triangle(10,8);

Figure figref;

figref=r;

System.out.println("area of rectangle is "+figref.area());

figref=r1;

System.out.println("area of rectangle is "+figref.area());

figref=t;

System.out.println("area of triangle is "+figref.area());

boolean b1=r1.equals(r);

System.out.println(b1);

}

}

// write a program to demonstrate override.

import java.lang.*;

class SuperClass

{

int i,j;

SuperClass(int a,int b)

{

i=a;

j=b;

}

void show()

{

System.out.println("i,j value's are"+i+","+j);

}

}

class SubClass Extends SuperClass

{

int k;

SubClass(int a,int b,int c)

{

super(a,b);

k=c;

}

void show()

{

super.show();

System.out.println("k value is"+k);

}

}

class Overide

{

public static void main(String args[])

{

System.out.println("String args[]);

SubCLass s1= new SubClass(1,2,3);

s1.show();

}

}

// write a program on when the Constructors called automatically.

import java.lang.*;

class X

{

X()

{

System.out.println("this is X");

}

}

class Y extends X

{

Y()

{

System.out.println("this is Y");

}

}

class Z extends Y

{

Z()

{

System.out.println("this is Z");

}

}

class CallingCons

{

public static void main(String args[])

{

System.out.println("Constructors called automatically");

Z a= new Z();

}

}

// Write a program to demonstrate some Access Contrls in packages.

package p1;

public class protectio

{

int n=1;

private int n_pri=2;

protected int n_pro=3;

public int n_pub=4;

public Protection()

{

system.out.println("base constructor");

system.out.println("n="+n);

system.out.println("n_pri="+n_pri);

system.out.println("n_pro="+_pro);

system.out.println("n_pub="+n_pb);

}

}

package p1;

public class Derived extends Protection

{

public Derived()

{

system.out.println("Desrived constructor");

system.out.println(n="+n);

//system.out.println("n_pri="+n_pri);

system.out.println("n_pro="+n_pro);

system.out.println("n_pub="+n_pub);

}

}

package p1;

public class SamePackage

{

samePackage()

{

protection p=new Protection();

system.out.println("same package constructor");

system.out.println("n="+p.n);

//system.out.println("n_pri="+p.n_pri);

system.out.println("n_pro="+p.n_pro);

sytem.out.println("n_pub="p.n_pub);

}

}

package p2;

public class Protection2 extends p1.Protection

{

public Protection2()

{

systemout.println("Derived other package constructor");

//system.out.println("n="+n);

//system.out.println("n_pri="+n_pri);

system.ot.println("n_pro="+n_pro);

system.out.println("n_pub="+n_pub);

}

}

package p2;

public class OtherPackage

{

OtherPackage()

{

p1.Protection p=new p1.Protection();

system.out.println("other package constructor");

//system.out.println("n="+p.n);

//system.out.println("n_pri="+p.n_pri);

//system.out.println((n_pro="+p.n_pro);

system.out.println("n_pub="+p.n_pub);

}

}

package p1;

public class Demo

{

public static void main(string[]args)

{

protection ob1=newprotection();

Derived ob2=new Derived();

SamePackage ob3=new SamePackage();

}

}

package p2;

public class Demo

{

public static void main(String[]args)

{

Protection2 ob1=new Protection2();

otherPackage ob2=new OtherPackage();

}

}

//write a program to demonstrate Uncaught Exception at Runtime.

classException1

{

public static void main(String[]args)

{

system.out.println("Exception");

int d=0;

int a=32/0;

}

}

//write program to demonstrate try and catch.

package ExceptionPack;

publicclass Arithmetic

{

public void subroutine(Stringxx[])

{

try

{

int d=xx.length;

int a=10/d;

system.out.println("argument is"+xx[0]);

int c[]={1};

//int[]c=new int[42];

c[32]=99;

}

catch(AritmeticException ae)

{

system.out.println("present ecception is"+ae);

system.out.println("division bu zero");

}

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println("present exception is "+e);

}

System.out.println("after catch block");

}

}

import ExceptionPack.Arithmetic;

class Exception2

{

public static void main(String args[])

{

System.out.println("Exception2");

Arithmetic a1=new Arithmetic();

a1.subroutine(args);

}

}

// write a program to demonstrate how to create a our own Exception class.

import java.lang.*;

class MyException extends Exception

{

private int detail;

MyException(int a)

{

detail=a;

}

public String toString()

{

return "MyException["+detail+"]";

}

}

class ExceptionDemo

{

void compute(int a) throws MyException

{

System.out.println("called comput("+a+")");

if(a>10)

{

throw new MyException(a);

}

System.out.println("Normal exit");

}

}

class MyExceptionDemo

{

public static void main(String args[])

{

System.out.println("MyExceptionDemo");

try

{

ExceptionDemo e1=new ExceptionDemo();

e1.compute(1);

e1.compute(20);

}

catch(MyException e)

{

System.out.println("caught"+e);

}

}

}

// write a program to demonstrate throw.

import java.lang.*;

class ThrowDemo

{

public void demproc(int a)

{

try

{

if(a>10)

{

throw new NullPointerException("xyz");

}else

{

throw new ArithmeticException("vimala");

}

}

catch(NullPointerExcption e)

{

System.out.println("caught inside demoproc("+a+")"+e);

}

}

public static void main(String args[])

{

try

{

ThrowDemo t1=new ThrowDemo();

t1.demoproc(4);

}

catch(Exception e)

{

System.out.println("Recaught Exeption in main()");

}

}

}

// write a program to demonstrate throws statement.

import java.lang.*;

class ThrowsDemo

{

public static void demoproc()throws ArithmeticException

{

System.out.println("inside demoproc()");

int a=0;

int d=42/a;

}

public static void main(String args[])

{

try

{

demoproc();

}

catch(Exception e);

{

System.out.println(e);

}

}

}

// write a program to demonstrate throw and throws statements.

import java.lang.*;

class ThrowThrows

{

public static void demoproc()throws ArithmeticException

{

System.out.println("inside demoproc()");

throw new ArithmeticException("/by zero");

}

public static void main(String args[])

{

try

{

demoproc();

}

{

System.out.println(e);

}

}

}

// write a program to demonstrate Current Thread.

import java.lang.*;

class CurrentThreadDemo

{

public static void main(String args[])

{

Thread t=Thread.currentThread();

System.out.println("Current Thread:"+t);

t.setName("MyThread");

System.out.println("after name change:"+t);

try

{

for(int n=5;n>0;n--)

{

System.out.println(n);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("Main Thread interrupted");

}

}

}

// write to create a thread by extending a Thread class.

import java.lang.*;

class NewThread1 extends Thread

{

NewThread1()

{

super("my thread");

System.out.println("child thread"+this);

start();

}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println("child thread"+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted");

}

System.out.println("Exiting from child thread");

}

}

class ThreadExtendDemo

{

public static void main(String args[])

{

new NewThread1();

try

{

for(int i=5;i>0;i--)

{

System.out.println("main thread"+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("Exiting from main thread");

}

}

// write a program to create a thread by implementing Runnable Interface.

import java.lang.*;

class NewThread implements Runnable

{

Thread t;

NewThread()

{

t=new Thread(this, "my thread");

System.out.println("child thread"+t);

t.start();

}

public void run()

{

try

{

for(int i=;i>0;i--)

{

System.out.println("child thread"+i);

Thread.sleep(500);

}

}

catch(InterruptedException e)

{

System.out.println("child interrupted");

}

System.out.println("Exiting from child thread");

}

}

class ThreadRunnableDemo

{

public static void main(String args[])

{

new NewThread();

try

{

for(int i=5;i>0;i--)

{

System.out.println("main thread"+i);

Thread.sleep(1000);

}

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("Exiting from main thread");

}

}

// write a program to demonstrate Thread Priority.

import java.lang.*;

class A extends Thread

{

public void run()

{

System.out.println("Thread A started");

for(int i=1;i<=4;i++)

}

System.out.println("From Thread A"+i);

}

System.out.println("Exited from Thread A");

}

}

class B extends Thread

{

public void run()

{

System.out.println("Thread B started");

for(int i=1;i<=4;i++)

{

System.out.println("From Thread B"+i);

}

System.out.println("Exited from Thread B");

}

}

class C extends Thread

{

public void run()

{

System.out.println("Thread C started");

for(int i=1;i<=4;i++)

{

System.out.println("From Thread C"+i);

}

System.out.println("Exited from ThreadC");

}

}

class ThreadPriorityDemo

{

public static void main(String args[])

{

A ta=new A();

B tb=new B();

C tc=new C();

tc.setPriority(Thread.MAX_PRIORITY);

tb.setPriority(ta.getPriority()+1);

ta.setPriority(Thread.MIN_PRIORITY);

System.out.println("start Thread A");

ta.start();

System.out.println("start Thread B");

tb.start();

System.out.println("start Thread C");

tc.start();

}

}

// write a program to demonstrate join(), wait() and notify().

import java.lang.*;

class NewThread1 implements Runnable

{

Thread t;

String name;

boolean suspendFlag;

NewThread1(String tn)

{

name=tn;

t=new Thread(this,name);

System.out.println("New thread"+t);

suspendFlag=false;

t.start();

}

public void run()

{

try

{

for(int i=5;i>0;i--)

{

System.out.println(name+":"+i);

Thread.sleep(500);

synchronized(this)

{

while(suspendFlag)

{

wait();

}

}

}

}

catch(InterruptedException e)

{

System.out.println(name+":interrupted");

}

System.out.println("Exiteng from "+name+"thread");

}

void mySuspend()

{

suspendFlag=true;

}

synchronized void mythread()

{

suspendFlag=false;

notify;

}

}

class SuspendedResume

{

public static void main(String args[])

{

NewThread1 ob1=new NewThread1("one");

NewThread2 ob2=new NewThread2("two");

try

{

Thread.sleep(1000);

ob1.mySuspend();

System.out.println("suspending thread one");

ob1.myresume();

System.out.println("resuming thread one");

Thread.sleep(1000);

ob2.mySuspend();

System.out.println("suspending thread two");

ob2.myresume();

System.out.println("resuming thread two");

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

System.out.println("Exiting from main thread");

try

{

ob1.t.join();

ob2.t.join();

}

catch(InterruptedException e)

{

System.out.println("main interrupted");

}

}

}

//char in java

import java.lang.*;

class Char

{

public static void main(String args[])

{

//char ch1;

//ch1='A';

//System.out.println("before increment" +ch1);

//ch1++;

int a;

a='3';

a++;

System.out.println("after increment" +a);

}

}

//char demonstration

import java.lang.*;

class CharDemo

{

public static void main(String args[])

{

char ch1, ch2;

ch1=90;

ch2='y';

System.out.println("ch1 and ch2:");

System.out.println(ch1+" "+ch2);

}

}

//Serialization

import java.io.*;

class Student implements java.io.Serializable{

public String name;

public String address;

public transient int rollno;

public int roomNo;

}

public class SerializeExample {

public static void main(String[] args) {

Student e = new Student();

e.name = "Vishwanath";

e.address = "KPHB";

e.rollno = 98765;

e.roomNo = 123;

try {

FileOutputStream fileOut = new FileOutputStream("student.ser");

ObjectOutputStream out = new ObjectOutputStream(fileOut);

out.writeObject(e);

out.close();

fileOut.close();

System.out

.println("Object is serialized & stored in 'student.ser'");

} catch (IOException ie) {

ie.printStackTrace();

}

}

}

//De-serialization

import java.io.*;

public class DeserializeExample {

public static void main(String[] args) {

Student e = null;

try {

FileInputStream fileIn = new FileInputStream("Student.ser");

ObjectInputStream in = new ObjectInputStream(fileIn);

e = (Student) in.readObject();

in.close();

fileIn.close();

} catch (IOException i) {

i.printStackTrace();

return;

} catch (ClassNotFoundException c) {

System.out.println("Student class not found");

c.printStackTrace();

return;

}

System.out.println("Deserialized Student...");

System.out.println("Name: " + e.name);

System.out.println("Address: " + e.address);

System.out.println("Roll no: " + e.rollno);

System.out.println("Room No: " + e.roomNo);

}

}

// Passing object as a argument

import java.lang.*;

class Test1

{

int a,b;

Test1(int i,int j)

{

a=i;

b=jh;

}

boolean equals(Test1 o)

{

if(o.a==a && o.b==b)

return true;

else

return false;

}

}

class PassOb

{

public static void main(String args[])

{

Test1 ob1=new Test1(100,200);

Test1 ob2=new Test1(100,200);

Test1 ob3=new Test1(-1,-1);

System.out.println("ob1==ob2 "+ob1.equals(ob2));

System.out.println("ob1==ob3 "+ob1.equals(ob23));

}

}

//Constructors

public class Constructors{

int ConstructorsAge;

public Constructors(String name){

// This Constructors has one parameter, name.

System.out.println("Passed Name is :" + name );

}

public void setAge( int age ){

ConstructorsAge = age;

}

public int getAge( ){

System.out.println("Constructors's age is :" + ConstructorsAge );

return ConstructorsAge;

}

public static void main(String []args){

/* Object creation */

Constructors myConstructors = new Constructors( "tommy" );

/* Call class method to set Constructors's age */

myConstructors.setAge( 2 );

/* Call another class method to get Constructors's age */

myConstructors.getAge( );

/* You can access instance variable as follows as well */

System.out.println("Variable Value :" + myConstructors.ConstructorsAge );

}

}

//instance variable

//It is a variable inside class out side a method without a static keyword.

import java.io.*;

public class instance_var{

// this instance variable is visible for any child class.

public String name;

// salary variable is visible in instance_var class only.

private double salary;

// The name variable is assigned in the constructor.

public instance_var (String empName){

name = empName;

}

// The salary variable is assigned a value.

public void setSalary(double empSal){

salary = empSal;

}

// This method prints the instance_var details.

public void printEmp(){

System.out.println("name : " + name );

System.out.println("salary :" + salary);

}

public static void main(String args[]){

instance_var empOne = new instance_var("Anji");

empOne.setSalary(1000);

empOne.printEmp();

}

}