• +1 240 877 8161
  • info@thePracticalIT.com
Practical Information Technology
  • Notifi
  • Canvas
    • HOME
    • Start Here
    • Courses
    • EVENTS
      • EVENTS LIST
      • EVENTS GRID
    • BLOG
    • CONTACT
    • IT Courses
  • +1 240 877 8161
  • info@thePracticalIT.com
  • Profile
  • Login | Registration Popup Link
Practical Information Technology
  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

intance vs class vs local variables Java

Homepage - Programming Exercises - intance vs class vs local variables Java

Pract IT

by Practical IT
Posted on 22 Jun, 2021
in
Programming Exercises
8 Comment

For the sake of discussion let’s have the following class

/**
 * Simple java class for instance, local and class variable demonstration  
 * 
 * @author Practical IT <info@thepracticalit.com>
 *
 */
public class Java {
           //instance variables are those outside of methods in class
           //they accessible in non-static methods in the class.
	   private int iAmInstanceIntVariable;
           //static variables don't need object to be accessed, just class
	   private static int iAmClassVariable = 200;

	   public float iAmInstanceMethodReturningNothing() {
		   //examples of local variables that are accessed within the method/function only
	     int iAmLocalVariableAccessibleInMethodOnly = 90;
	     float iAmLocalFloatVariableAcessibleInThisMethodOnly = 120.9f;
	     //their sum can be returned but the variables are accessed in this method only.
	     return iAmLocalFloatVariableAcessibleInThisMethodOnly+ iAmLocalVariableAccessibleInMethodOnly;
	   }

	   public int iAmStaticMethodReturningInt() {
	     int iAmLocalintInitializedWithFive = 5;
	     return iAmLocalintInitializedWithFive;
	   }

	   public void setInstanceInt(int iAmClassVarible) {
		   this.iAmInstanceIntVariable = iAmClassVarible;
	   }
	   
	   public int getInstance() {
		   return this.iAmInstanceIntVariable;
	   }
	   
	   public static int getStaticVariable() {
		   //this.iAmInstanceIntVariable = 4; this is invalid  static method can't access instance variable.
		   return iAmClassVariable;
	   }

	   public  static void main(String[] a) {
	     //create an instance, java is instance of Java class
	     Java java = new Java();
	     java.setInstanceInt(23); //object can access method.
	     System.out.println(java.getInstance());//get instance is accessed only from 'instance'
	     System.out.println(Java.iAmClassVariable);//this is what class variable mean, it is accessed from the class not from instance
	     //iAmStaticMethodReturningInt is a method returning a local variable's value
	     System.out.println(java.iAmStaticMethodReturningInt()); //method is also accessed from instance only
	     System.out.println(Java.getStaticVariable()); //method that is accessed only from the class - static 
	   }
}

*This is a light version of definition. There could be cases more complex implementation may override the common behavior.

Instance Variables

  • Declared outside of the methods but inside the class.
  • Can be accessed in the non-static methods of the class or children based on the right access modifier.
  • Can have private, public, protected.. access modifiers
  • declared as access modifier data type variable name [= value]
  • Are available/visible for the “instance” of the class. Different objects/instances can have different values for the variables.

Static Variables / Class variables

  • Declared outside of the methods but inside the class.
  • Declared as access modifier static data type variable name [=value]
  • Accessed through the class and not through “instance”
  • All objects have access/visibility to the variable and see the same value

Local Variables

  • Declared within methods of the class, including the constructor.
  • Visibility/access is limited to the method that declared it.
Tags: class instance local methods private static
User Avatar
Article by Practical IT

Works at Practical IT. Passionate about Technologies, Business and News.

Previous Story
A Page with Checkboxes
Next Story
Comment Character Counter JavaScript

Related Articles

IT Salary

February 2024 Class Orientation and Information.

Join us for great discussion on the upcoming IT technology...

Web development code

Full Stack Dev Feb 28 Class

Full Stack Developer Full Stack is a highest in demand...

Leave your comment Cancel Reply

(will not be shared)

SearchLeave your keyword

Change your life through Information Technology career today. Thousands have done it – it is your turn now. All you need is a passion to change your life and provide better for yourself and your family. Practical IT takes the rest.

QUICK LINKS

  • Home
  • Courses
    • FEATURED COURSES
      • Web Development
      • Database Development
      • Business Intelligence
    • TRAINING
      • How long does it take?
      • IT Training Methods
      • Hours Per week
      • Payments
    • IT Courses
  • Tips
  • Contact
  • Notifi
  • Start Here

SUBSCRIBE EMAIL

Subscribe to our email list and get tips on IT jobs, career changes openings, seminars and more. We can also SMS you

GET IN TOUCH

You can call us, email us or even better visit us in our office at the heart of Silver Spring. Our Email - info@thePracticalIT.com

+1 240 877 8161

911 Silver Spring Ave #101 Silver Spring MD 20910

Practical IT - Copyright 2023.
  • Sign in

or
  • Login with Facebook
  • Login with Google

Forgot your password?

Lost your password? Please enter your email address. You will receive mail with link to set new password.

Back to login