Monday, January 23, 2012

static variable

/*
static most powerful and beautiful concept  in java .if use static, jvm will load at the time of class load.
 if we use static for a variable it treated as class level variable for every new object will not create new variable existing only used.
*/

public class Static_Variable
{
static String collegename="ghandi college";
public void method1()
{
//incomplete
}
}

static NOT applicable with modifiers

/*
RULE: static keyword not applicable with the
1.final
2. abstract
and class

*/

static applicable

/*
RULE: static keyword only applicable for variables , methods and block but not for classes
*/

public class Static_Demo
{
static int x=10;
static
{
System.out.println(x);
}
public static void main(String arg[])
{
System.out.println("static main method")
}
}

public class

/*
RULE: if we declared a class as public, we can access that class from any where
*/

public class Public_Class
{

}

protected class

/*
RULE: a class declared as protected, than it can be visible or access in the child classes of the same package and out side of the child classes
*/

protected class Protected_Class
{

}

default class

/*
RULE: if not declared a with public or protected, it treated as default class. a default class can access with in the package of the child classes but not out side of  the package.
*/

package com.default.pack;
class Default_Class
{

}


package com.default.pack;
import com.default.pack;
class Default_Class_Use
{
Default_Class dc=new Default_Class();
}


//

package com.other.pack;
import com.default.pack;
class Default_Class_Use
{
Default_Class dc=new Default_Class(); //compile time error
}

abstract class

/*
RULE: we can declare a class as abstract, if we declare a class as abstract that means all the methods in that class need not be abstract. that abstract class may be not with abstract methods or normal methods 
*/

public abstract class Abstract_Class
{
abstract method1();
abstract method2();
public void method3()
{
System.out.println("normal method");
}
}


Demo 2:

public abstract class Empty_Abstract
{

}

in final class

/*
RULE: if we declared a class as final that means in that class all the methods are by default final but variables are not final
And it can not support the inheritance concept. and run time polymorphism But it can support the static polymorphism
*/

public final class Final_Class
{
int x=10;
public void method1()
{
System.out.println(x);
x=20;
System.out.println(x);
}
public void method1(int x)
{
this.x=x;
System.out.println(x);
}
}
class Demo extends Final_Class
{
public void method1()
{
System.out.println("child");
}
}

class can applicable with modifiers


/*
RULE: class can applicable with the following modifiers are
1.public
2.final
3.abstract


*/

class canNOT applicable modifiers are

/*
RULE: class canNOT  applicable with the following modifiers are
1.private
2.protected
3.strictfp (only for methods)
4.synchronized (only for methods/blocks)
5.native (only for methods)
6.volatile (only for variables)
7. transient (only for variables)
8.static (only for methods/variables/blocks)


*/

class

/*
RULE : class is keyword by using class keyword we can achieve the encapsulation mechanism
in oops.
Definition: class is an way of creating user defined data types.
*/

class Class_Structure
{
static variables;
.
.
 instance variables;
.
.
{
//instance block
}
static
{
//static block
}
methods()
{
}
}

final NOT applicable 4


/*
RULE: final key word not applicable for the following modifiers only
1.abstract
2.
3

 in complete
*/

final applicable 4 only

/*
RULE: final key word applicable for the following modifiers only
1.public
2.private
3.protected
4.<<default>>
5.native
6.static
7.
 in complete
*/

final variable

/*
RULE: we can declare a variable as final and not necessary declare a variable until it will be use.
we can initialize the variable at the time of use it.
*/

public class Final_Variable
{
public static void main(String ar[])
{
final int x;
System.out.println("hello");   //no error its legal
x=10;
System.out.println(x);
}
}

final instance variable

/*
RULE:  we can declare instance variable as final but jvm wont provide any default values for instance variable, programmer have provide the values for final instance variables .
we can provide values in three ways
1. at the time of declaring the variable.
2. in constructor.
3.in instance block
*/

public class Final_Instance_Variable
{
final double d;
public static void main(String ar[])
{
System.out.println(x);     // compile time error
}
}

Way 1 : initialization value for final instance variable


public class Final_Instance_Variable
{
final double d=20.3;
public static void main(String ar[])
{
System.out.println(x);
}
}


Way 2 : initialization value for final instance variable


public class Final_Instance_Variable
{
final double d;
public Final_Instance_Variable()
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}



Way 3 : initialization value for final instance variable


public class Final_Instance_Variable
{
final double d;
{
d=20.3;
}
public static void main(String ar[])
{
System.out.println(x);
}
}



Final Static Variable

/*
RULE: we can declare a static variable as final , but jvm not provide any default values for final static variables
we have to provide before class load. we can provide in two ways in static block or by the time of declaring the variable.
*/

public class Final_Static_Variable
{
final static int x;
public static void main(String ar[])
{
System.out.println(x);   //error
}
}

Demo For Initialization for static final variable.(way 1)

 public class Final_Static_Variable
{
final static int x=10;
public static void main(String ar[])
{
System.out.println(x);   //error
}
}


Demo For Initialization for static final variable.(way 2)

 public class Final_Static_Variable
{
final static int x;
static{
x=10;
}
public static void main(String ar[])
{
System.out.println(x);   //error
}
}

abstract applicable for what

/*
RULE: abstract key word only applicable for CLASSES and METHODS not for variables
*/
public abstract class AbstractDemo
{
abstract int x;                             // illegal
abstract public void method1();
abstract public int method2();
}