The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java Access (Instance and Static methods)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java Access (Instance and Static methods) Posted: Jan 4, 2016 7:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java Access (Instance and Static methods)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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
  • Read: Java Tutorial : Java Access (Instance and Static methods)

    Topic: IntelliJ IDEA 15.0.3 EAP 143.1653 is Out Previous Topic   Next Topic Topic: Top 10 Big Data Trends in 2016 for Financial Services

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use