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) |
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."); } }
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(); } }
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:
No comments:
Post a Comment