Home > Java > javaTutorial > Types of Variables in Java

Types of Variables in Java

王林
Release: 2024-08-09 16:39:54
Original
900 people have browsed it

Types of Variables in Java

In Java we have three types of variable called instance, local and static

Note:- In Java we didn't support global variables

Instance variables

Instance variables are those variables which we define in the class out not in method

public class A {
   String name;
   String color;
}

Copy after login

in the above example in class A we defined some variables using data type in the above code snippet we had define instance variables inside the class but out side the method these variables are instance variables if you want access instance variables outside the class then you have to use access specifier public instance variables scope depended upon access specifier

Local variables

A variables used while method declaration passing method augments or parameter or declaring variables in side method body called as local variables

public int caluclate(int a, int b) {
  return a + b;
}
Copy after login

In above code snippet variables declare in method called as local variables you can only use this variable only inside the method

*Static variables *

static variables defined using static keyword after declaring static static keyword in front of your variable the variables called as static variables

public class B {
  static String name = "Ryan";
}
Copy after login

The above code snippet is a example of static variables

The above is the detailed content of Types of Variables in Java. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template