Posts

Showing posts from September, 2010

Non Static Variables

Can be accessed only by creating an instance.
It will be not be updated when a new instance of the object is created.

public class NonStaticVariables {
 public NonStaticVariables() {
        instancecount++;
    }

    private   int instancecount;
    public static void main(String args[]){
        NonStaticVariables nsv=new NonStaticVariables();
    System.out.println(nsv.instancecount);
  NonStaticVariables nsv1=new NonStaticVariables();
    System.out.println(nsv1.instancecount);

    }

}

Static Variables

Can Be Accessed directly in the main method.Static instance variable will be updated on creating a new instance of the  object.

public class StaticVaribles {

    public StaticVaribles() {
        instancecount++;
    }

    private static  int instancecount;
    public static void main(String args[]){
    System.out.println(instancecount);

    StaticVaribles sv=new StaticVaribles();
    System.out.println(instancecount);

        StaticVaribles sv1=new StaticVaribles();
    System.out.println(instancecount);
    }

}

/*output


run:
instancecount->0
instancecount->1
instancecount->2

*/

innerclasses

Inner Classes in java:
Inner classes are the nested classes.
example of a inner class:
class MyOuter{
class MyInner{
public void display(){
System.out.println("Inner class");
}
}
}


Type- 1:
aCCESS outer class variable from inner class
class MyOuter{
private int i=10;
class MyInner{
public void display(){
System.out.println("Accessing private member of the outer"+i);
}
}
}


Type-2:
Create an Instance of Inner class in outer class:
class MyOuter{
private int i=0;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
public void display(){
system.out.println("Accessing private memeber of the outer"+i);
}
}
}




Type-3:


Create an Instance of Inner claSS from outside outerclass


class MyOuter{
private int i=15;
public void makeInner(){
/*Creating an instance of the inner class in the outer class*/
MyInner in= new MyInner();
/*call the inner class method*/
in.display();
}
class MyInner{
pub…