[Java Silver] What are class variables instance variables and local variables?

We will summarize the differences between instance variables, local variables, and class variables.

static variable (class variable)

-A variable declared with ** static ** --Has a value common to all instances of the class. It is also called ** class variable ** because it has a value attached to the class.

Instance variables

Variables defined directly under the class ** and outside the ** method **

Local variables

A variable declared in ** in a method ** There is no problem even if you define a local variable with the same name as an instance variable or class variable **! !! !!

Check with code

public class Main{
 public static void main(String[] args){
  Party P1 = new Party();
  Party P2 = new Party();
  Party.budget = 10000;
  
  P1.participants = 5;
  P1.remain();  //budget outputs as 9500
  
  System.out.println(Party.budget); //9500 and output
  System.out.println(P2.budget); //9500 and output
  
  P2.participants = 10;
  P2.remain();  //8500 and output
  
  System.out.println(Party.budget);  //8500 and output
  System.out.println(P1.budget);   //8500 and output
 }
}



class Party {
   int participants; //Instance variables
   static int budget;  //static variable (class variable)

   void remain(){
       int expense = 100 * participants;  //Local variables
       budget -= expense;
       System.out.println(budget);
   }
}

**-When having a local variable ** with the same name as the instance variable **

public class Main{
 public static void main(String[] args){
  Party P1 = new Party();
  Party P2 = new Party();
  Party.budget = 10000;
  

  P1.remain();  //budget outputs as 8000
  
  System.out.println(Party.budget); //8000 and output
  System.out.println(P2.budget); //8000 and output
  

  P2.remain();  //6000 and output
  
  System.out.println(Party.budget);  //6000 and output
  System.out.println(P1.budget);   //6000 and output
 }
}

 class Party {
   int participants; //Instance variables
   static int budget;  //static variable (class variable)

   void remain(){
       int participants = 20;  //A local variable with the same name as an instance variable
       System.out.println(participants);
       budget -= participants*100;
       System.out.println(budget);
   }
 }  

Recommended Posts

[Java Silver] What are class variables instance variables and local variables?
[Java] Differences between instance variables and class variables
Why are class variables needed? [Java]
Java local variables are thread safe
[Java] What are overrides and overloads?
Java programming (static clauses and "class variables")
Java local class
[Rails] Check the instance variables and local variables of the file you are browsing
What I learned in Java (Part 2) What are variables?
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Java] Variables and types
[Introduction to Java] Variable scope (scope, local variables, instance variables, static variables)
Java programming (variables and data)
Java class definition and instantiation
Difference between variables and instance variables
[Environment variables] What are rails environment variables?
Difference between class and instance
What are practically final variables?
What are Ruby class methods?
[Java] What is class inheritance?
About Java class variables class methods
About instance variables and attr_ *
[Java basics] What is Class?
What are Java metrics? _Memo_20200818
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Java] A class is an OS, and an instance is a virtual computer.
Difference between instance method and class method
Java Primer Series (Variables and Types)
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Organize classes, instances, and instance variables
[Java] How to use static modifiers (What are static final and static import)
Difference between instance variable and class variable
Java array variables are reference types
StringBuffer and StringBuilder Class in Java
What I learned with Java Silver
Activity transition with JAVA class refactoring and instance experimented on Android side
What is a class in Java language (3 /?)
Java starting from beginner, variables and types
Java Silver exam procedure and learning method
java jshell and var are too recommended!
Difference between Ruby instance variable and local variable
What is a class in Java language (1 /?)
What is Java and Development Environment (MAC)
What is a class in Java language (2 /?)
What are the updated features of java 13
What are JDK, Oracle JDK, OpenJDK, Java SE?
[Java] Instance method, instance field, class method, class field, constructor summary
Instance concept, class type variables, constructors, static members
[Java] How to use FileReader class and BufferedReader class
[Ruby] Basic knowledge of class instance variables, etc.
When there are environment variables in Java tests
[Java SE 11 Silver] Arrays class method summary [Java beginner]
Java and Swift comparison (1) Source control / Scope / Variables
What is the volatile modifier for Java variables?
What are the advantages of DI and Thymeleaf?
[Java] How to use Calendar class and Date class