Home     Blog

Java Variables

An instance variable is a variable that is related to a single instance of a class. Each time an instance of a class is created, the system creates one copy of the instance variables related to that class.

An instance variable is declared inside a class, but not within a method. A variable that is defined inside a class and a method is known as a local variable.

The opposite of an instance variable is a static variable, also referred to as a class variable. A static variable exists across instances of a class.

By default, all variables are created as instance variables.

Static Variablesinstance variable 150x150 Java Variables

A static variable, also referred to as a class variable, is a variable that exists across instances of a class.

The opposite of a static variable is an instance variable, which is a variable related to a single instance of a class. Each time an instance of a class is created, the system creates one copy of the instance variables related to that class.

By default, all variables are created as instance variables. To make a class variable, one must explicitly declare the variable as static.

Final Variables

A final variable is a variable that has been initialized to a fixed value that cannot be changed after initialization.

In older programming languages, a final variable would be called a constant.

Variable Naming Constraints

Java variable names must start with one of the following characters:

  • Letter
  • Underscore
  • Dollar sign

After the first character, Java variable names can contain numbers.

Java Variable Naming Reserved Words

Java variable names cannot be one of the following Java reserved words:

  • assert
  • boolean
  • break
  • byte
  • case
  • catchjava variable naming constraints Java Variables
  • char
  • class
  • const
  • continue
  • default
  • do
  • double
  • else
  • extends
  • false
  • final
  • finally
  • float
  • for
  • goto
  • if
  • implements
  • import
  • instanceof
  • int
  • interface
  • long
  • native
  • new
  • null
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • strictfp
  • super
  • switch
  • synchronized
  • this
  • throw
  • throws
  • transient
  • true
  • try
  • void
  • volatile
  • while
VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • How to Declare a Constant in Java

    Java does not directly support constants. However, a static final variable is effectively a constant. The static modifier causes the variable to be available without loading an instance of the class where it is defined. The final modifier causes the variable to be unchangeable. Naming Standards for Java Constants Java constants are normally declared in [...]...


  • Java Method

    A Java method is a set of Java statements that can be included inside a Java class. Java methods are similar to functions or procedures in other programming languages. There are many different types of Java methods: Final Method Public Method Private Method Package Method Instance Method Protected Method Static Method Main Method The Main [...]...


  • Java vs. C++

    C++ supports pointers; Java does not (Java does have object references) C++ supports mutiple inheritance directly; Java supports multiple inheritance only through interfaces C++ supports operator overloading; Java does not C++ supports global variables; Java does not (sort of) C++ supports #define; Java does not C++ supports typedef; Java does not C++ supports enum; Java [...]...


  • Java Class

    A Java class is a group of Java methods and variables. Each Java source code file can contain one public class. The name of this public class must match the name of the Java source code file. If the public class is called “ballistics,” then the filename would be “ballistics.java.” Example Java Class class Ammunition [...]...


  • Java Data Types

    Java has two groups of data types, primitive data types and object references. Java Primitive Data Types Data Type Purpose Contents Default Value* boolean Truth value true or false fales char Character Unicode characters u0000 byte Signed integer 8 bit two’s complement (byte) 0 short Signed integer 16 bit two’s complement (short) 0 int Signed [...]...