Monday, 4 January 2016

Java Tutorial : Java Access (Instance and Static methods)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=uSfECHNosbE&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Access (Instance and Static methods) 
Java Tutorial : Java Access (Instance and Static methods) 
Java Tutorial : Java Access (Instance and Static methods) 
InstanceMethodTest.java
public class InstanceMethodTest
{
    int instanceVar = 10;
    static String staticVar = "Welcome";

    /*
     * Instance methods can access instance variables and instance methods
     * directly.
     * 
     * Instance methods can access class variables and class methods directly.
     */
    public void instanceMethod1()
    {
        System.out.println("instanceMethod1 is called.");
        System.out.println("instanceVar : " + instanceVar);
        instanceMethod2();
        
        System.out.println("staticVar : " + staticVar);
        staticMethod();
    }

    public void instanceMethod2()
    {
        System.out.println("instanceMethod2 is called.");
    }
    
    public static void staticMethod()
    {
        System.out.println("staticMethod is called.");
    }

}
ClassMethodTest.java
public class ClassMethodTest
{
    int instanceVar = 10;
    static String staticVar = "Welcome";

    public void instanceMethod1()
    {
        System.out.println("instanceMethod1 is called.");
    }


    /*
     * Class methods can access class variables and class methods directly.
     * 
     * Class methods cannot access instance variables or instance methods
     * directly—they must use an object reference.  Also, class methods cannot
     * use the this keyword as there is no instance for this to refer to.
     */

    public static void staticMethod()
    {
        System.out.println("staticMethod is called.");

        System.out.println("staticVar : " + staticVar);
        staticMethod();

        System.out.println("instanceVar : " + instanceVar);
        instanceMethod1();

        System.out.println(this.instanceVar);
        
        ClassMethodTest classMethodTest = new ClassMethodTest();
        
        System.out.println("instanceVar : " + classMethodTest.instanceVar);
        classMethodTest.instanceMethod1();      

    }

}
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ClassMethodDemo-AccessApp.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ClassMethodDemo-AccessApp/ClassMethodDemo

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/41d9dfe14fcf9b8b617da7211fce54dcabe49f5c/BasicJava/ClassMethodDemo-AccessApp/ClassMethodDemo/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment