Java - Variable

Java Conceptuel Diagram

About

There are several kinds of variables:

  • Member variables in a class—these are called fields.
  • Variables in a method or block of code—these are called local variables.
  • Variables in method declarations—these are called parameters.
  • Reference variable : an object

Scope

Package

Field

Member variables in a class are called fields.

public class MyMain {

private static boolean myField;

.......

Method

Local Variables

Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

boolean myLocalVariable = true;

Parameter

Java - Parameter: The important thing to remember is that parameters are always classified as “variables” not “fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers).

public static void myFunction(boolean myParameter) {
  ...........
}

How to

Declare a variable

To declare a variable, you write:

type name;

This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.

Simply declaring a reference variable does not create an object, you have to use the new operator. The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory.

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - (Field|Member Variable)

A field is a variables in a class and they are used to contain state information. They are also known as member variable. A member that is not declared as static is implicitly an instance member. ...
Java Conceptuel Diagram
Java - Annotations

Since Java version 1.5. An annotation is an element of the java programming language (modifier or Metadata tag) that can be associated with: Java classes, interfaces, constructors, methods,...
Simple Class
Java - Class (Definition)

A java/lang/Classclass provides the blueprint for objects; you create an object from a class. All classes are derived from the Object class. A class declaration names the class and encloses the class...
Java Conceptuel Diagram
Java - Interface (Class) - Data Encapsulation

see In the Java programming language, an interface is a reference type, similar to a class, that can contain only: constants, method signatures, and nested types. There are no method bodies....
Java Conceptuel Diagram
Java - Object (instance of a class)

An java/lang/Objectobject: stores its state in fields and exposes its behaviour through methods (functions in some programming languages). Methods operate on an object's internal state and serve...
Java Conceptuel Diagram
Java - Parameter

Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match...
Java Conceptuel Diagram
Java - Static Modifier

in Java. The Java programming language supports static methods. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for...



Share this page:
Follow us:
Task Runner