stu nicholls dot com | menu - Professional dropdown #3

click here

Java Classes and Methods

Java class contains

Class definition
Instance, class, local Variables

Method definition
Name of the method
List of parameters
Body of methods
Return type

Definition of a class

General form of a class
Class classname
{
  type instance-variable1;
  type instance-variable2;
    ....
  type instance-variablen;

  type methodname1(parameter list)
  {
    body of the method
  }

  type methodname2(parameter list)
  {
    body of the method
  }
    ....
  type methodnamen(parameter list)
  {
    body of the method
  }
}

The data or variables within the class are called as instance variables. The data or variables and methods which are within the class are collectively called as members of the class.
variable declaration:

    int x,y;
    char c;
    float m;

Methods

Methods are defined as follows.
They contain

Return type Name of the method (A list of parameters)
{
  Body of the method
}

e.g.:
  void dispValue( )
  {
    int x = 5;
    int y = 10;
    int z;
    z=x+y;
    System.out.println("After addition" + z);
  }

void is the retutn type and dispValue is the name of the method.
The name of the method and list of parameters is called as the signature of the method.

Note 1: Return type of the method is not part of the method's signature.

Note 2: In the above program the declaration of local variables hides (or takes precedence over) the value of instance or class variables. However, the values of instance or class variables can be accessed by using the keyword this along with the variable name.

Example for a class

class Employee
{
  int id;
  float salary;
  String name ;
  void ReadData()
  {
    ....
  }
  void DispData()
  {
    ....
  }
}

Program :

public class PrintPoint
{
  public int x = 5;
  public int y = 10;
  void dispoint()
  {
    System.out.println("Printing the Coordinates");
    System.out.println(x +" "+y);
  }
  public static void main(String args[])
  {
    PrintPoint point = new PrintPoint();
    point.dispoint();
  }
}

Access Modifier

Variables

Local Variable

Instance Variables

Click here for example

Program :

public class TestThis
{
  int x = 10;
  static int y = 20;
  final int z = 30;
  void dispvalue()
  {
    int x = 5;
    int y = 10;
    int z = 25;
    System.out.println("Printing Local variables");
    System.out.println(x +" " +y+ " "+z);
    System.out.println("Printing Instance variables");
    System.out.println(this.x +" " +this.y + " "+this.z);
  }
  public static void main (String args[])
  {
    TestThis temp = new TestThis();
    temp.dispvalue();
    System.out.println("Printing values in main");
    System.out.println(temp.x+ " "+temp.y+" "+temp.z);
  }
}

Class (static) Variables


Example:
class BankAccount
{
  static int bankID;// class variable.
  int amount, rate; // instance variable
  int balance; // instance variable
  void withdraw(int amt)
  {
    balance -= amt; // local variable
  }
  ....
}

Click here for example

Example: local, instance and static variable

public class LisVariable
{
  int height = 180;
  static float salary = 5000;
  final String name = "Krithika";
  void disp()
  {
   int height = 190;
   float salary = 6000;
   String name = "Gokul";
   System.out.println("\n\nPrinting Local variables");
   System.out.println(height +" " +salary + " "+name);
   System.out.println("\n\nPrinting Instance variables");
   System.out.println(this.height +" " +this.salary + " "+this.name);
  }
  public static void main (String args[])
  {
   LisVariable temp = new LisVariable();
   temp.disp();
   System.out.println("\n\nPrinting values in main");
   System.out.println(temp.height+ " "+temp.salary+" "+temp.name);
  }
}

Instance

Instance methods: Instance methods can be used only with respect to the instance of class.

class MyClass
{
  ......
  void testMethod()
  {
    ......
  }

  public static void main(String args[])
  {

    MyClass cls=new MyClass();
    cls.testMethod();
  }
}

Class methods


class MyClass
  {
    .....
  static void testMethod()
  {
    .....
  }
  public static void main(String args[])
  {
    MyClass.testMethod();
  }
}

Program: Instance and static methods

class InstMethods
{
  int i=5;
  float f=55.5f;

  static void dispvalue()
  {
    System.out.println("\n\nThis is static method");
  }
  void disp()
  {
    System.out.println("\n\nWe are testing an instance method");
    System.out.println("\n\nvalues are "+i+" "+f);
  }

  public static void main(String args[])
  {
    InstMethods ob1=new InstMethods();
    //InstMethods.dispvalue();
    dispvalue();
    ob1.disp();
  }
}

Command line arguments:

Click here for example

Program: Command Line Argument

class CmdLine
{
  public static void main(String args[])
  {
    int i;
    System.out.println("\n\n"+args.length+ " arguments passed from the command line and they are\n");
    for (i=0;i<args.length;i++)
    {
      System.out.println(i+"="+args[i]+"\n");
    }
  }
}

Method Overloading

Program for overloading methods

class OvrLoad
{
  public int calc(int x, int y)
  {
    return x+y;
  }
  public float calc(int x, float y)
  {
    return y/x;
  }
  public void calc(int x, String str)
  {
    int twice;
    twice=x * x;
    System.out.println("\n"+str+" " + twice);
  }
  public static void main(String args[])
  {
    OvrLoad ob1=new OvrLoad();
    System.out.println("\nValues added "+ob1.calc(5,6));
    System.out.println("\nValues divided " + ob1.calc(5,6.0f));
    ob1.calc(5,"Square of a value");
  }
}

Method Overriding

Click here for example 1

Click here for example 2

Inheritance: Sub class

Click here for example

Program: Subclass, Superclass

class TestSuper
{
  int i;
  long fact=1;
  void factFind(int n)
  {
    for(i=1;i<=n;i++)
    {
      fact=fact*i;
    }
  }
  void disp()
  {
    System.out.println("\n\nFactorial is calculated from super class and it is "+fact);
  }
}
class TestSub extends TestSuper
{
  void display()
  {
    System.out.println("\n\n I am in subclass");
  }
  public static void main(String args[])
  {
    TestSub ob=new TestSub();
    ob.display();
    ob.factFind(4);
    ob.disp();
  }
}

Keyword 'this'

'this' is used to refer to instance variables and methods of the current class.

class Box
{
  int width;
  int height;
  int depth;
  Box(int width, int hieght, int depth)
  {
    this.width = width;
    this.height = height;
    this.depth = depth;
  }
  ....
}

Keyword 'super'

Has 2 forms Calls the superclass' constructors used to access a member of the superclass that has been hidden by a member of a subclass.

class SuperClass
{
  int index;
  SuperClass(String name)
  {
    .....
  }
  ....
}
class Sub extends SuperClass
{
  int index;
  Sub(String s)
  {
    super(s);
    void print(this.index, super.index);
  }
  ....
}

Java Constructor

class test
{
  int i, j;
  test()
  {
    i=0;
    j=0;
  }
  test(int x, int y)
  {
    i=x;
    j=y;
  }
  void disp()
  {
    System.out.println("\n i="+i+ "j="+j);
  }
  public static void main(String args[ ])
  {
    test ob1=new test();
    test ob2=new test(6,7);
    ob1.disp();
    ob2.disp();
  }
}

Click here for example

Final classes, methods and variables

The final modifier indicates that the entity cannot be changed.


Class a
{
  ....
  Final void math()
  {          }
}

Final class a
{
  void math()
  {          }
}

Abstract classes

Interface:

Packages

Importing a Class from an Existing Java Package


import myJava.Account.myAccount; // imports the myAccount class only

                                                  OR

import myJava.Account.*; // imports all classes in Account package

Setting CLASSPATH to Point to Java Classes & Packages

Our aim is to provide information to the knowledge seekers. 

comments powered by Disqus
click here
click here
click here
click here
click here
click here
Footer1