wellnwill Marathahalli Bangalore

Method Practice Sheet


class Test1
{
 int a=10,b=20;
 
 void add()
 {
  System.out.println(a+b);
 }
 
 public static void main(String args[])
 {
  Test1 t1=new Test1();
  t1.add();
 }
 }

 
 
 
 
 
 
class Test
{
 void add()
 {
  System.out.println("0 parameter");
 }
 void add(int a)
 {
  System.out.println("1 parameter");
 }
 void add(int a,int b)
 {
  System.out.println("2 parameters");
 }
 public static void main(String args[])
 {
  Test t1=new Test();
  t1.add();
  t1.add(1);
  t1.add(1,2);
 }
}

class Test
{
 int add(int a,int b)
 {
     return a+b;
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  int z=t1.add(1,2);
  System.out.println(z);
 }
}

class Test
{
 int add(int a,int b)
 {
     return a+b;
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  System.out.println(t1.add(1,2));
 }
}



class Test
{
 int a,b;
 
 void insert(int c,int d)
 {
    a=c;
    b=d;	
 }
 
 void show()
 {
	System.out.println(a);
	System.out.println(b);
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  t1.insert(10,20);
  t1.show();
 }
}
class Test
{
  int i;
 
 static void show()
  {
  System.out.println(i);
  }
  
public static void main(String args[])
 {
  Test.show();
 }
}
Note: we are accessing object level variable 
inside static method. As we know, object level 
variable gets memory inside object once object 
will be created.






class Test
{
 int a,b;
 
 void insert(int a,int b)
 {
    a=a;
    b=b;	
 }
 
 void show()
 {
	System.out.println(a);
	System.out.println(b);
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  t1.insert(10,20);
  t1.show();
 }
}
class Test
{
	
double add(int a,double b)
 {
     return a+b;
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  double z=t1.add(1,2.2);
  System.out.println(z);
 }
}









class Test
{
 static int i;
 
 static void show()
 {
  System.out.println(i);
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  t1.show();
 }
}

class Test
{
 static int i;
 static void show()
 {
  System.out.println(i);
 }
public static void main(String args[])
 {
  Test.show();
 }
}





class Test
{
 static int i;
  void show()
 {
  System.out.println(i);
 }
 
 public static void main(String args[])
 {
  Test  t1=new Test();
  t1.show();
 }
}


class Test
{
 static String cname="nokia";
 void changeName(String n)
 {
    cname=n;
 }
public static void main(String args[])
 {
  Test  t1=new Test();
  System.out.println(t1.cname);
  t1.changeName("microsoft");
  System.out.println(t1.cname);
 }
}


class Test
{
	
 static String cname="nokia";
 
 static void changeName(String n)
 {
    cname=n;
 }
 
public static void main(String args[])
 {
  System.out.println(Test.cname);
  Test.changeName("microsoft");
  System.out.println(Test.cname);
 }
}
class Test
{
	
void display()
 {
  System.out.println(t); 
 }
 
public static void main(String a[])
 {
 Test t=new Test();
  System.out.println(t);
  t.display();
 }
}
Output-CTE


class Test
{
static Test t=new Test();
	
void display()
 {
  System.out.println(t); 
 }
 
public static void main(String a[])
 {
  System.out.println(t);
  t.display();
 }
}

class Test
{
void add(float f)
{
 System.out.println("with float parameter");
}

public static void main(String args[])
{
  Test t1=new Test();
  t1.add(10.5);
  } 
}
Output-CTE



class Test
{
  int i;
 
 static void show(Test t)
 {
  System.out.println(t.i);
 }
 
 public static void main(String args[])
 {
  Test  t1=new Test();
  t1.show(t1);
 }
}








class Test
{
 int a,b;
 
 void insert(int a,int b)
 {
    this.a=a;
    this.b=b;	
 }
 
 void show()
 {
	System.out.println(a);
	System.out.println(b);
 }
 
 public static void main(String args[])
 {
  Test t1=new Test();
  t1.insert(10,20);
  t1.show();
 }
}

Footer with Map

Top