Java Variable type

Source: Internet
Author: User

In Java, all variables must be declared before they can be used. The basic form of a variable declaration is as follows:

Type identifier [= value][, identifier [= value] ...];

type is one of the data types of java. The identifier is the name of the variable. Declare multiple variables of the specified type with a comma-delimited list.

Here are a few examples of variable declarations of various types. It is important to note that they may also include initialization.

Int a, b, c;         // declares three  ints, a, b, and c.int d = 3, e, f = 5; //  declares three more ints, initializing                      // d and  f.byte z = 22;         // initializes  Z.double pi = 3.14159; // declares an approximation of pi.char  x =  ' x ';        // the variable x  has the value  ' x '.

This chapter explains the various variable types available in the Java language. There are three types of variables in Java:

    • Local variables

    • Instance variable

    • class/Static variable

Local variables:
    • Local variables are declared in a method, constructor, or block.

    • When you create a method for a local variable, the constructor or block is entered, and once you exit the method, the constructor or the variable in the block is destroyed.

    • Access modifiers cannot be used with local variables.

    • Local variables are visible only within declared methods, constructors, or blocks.

    • Local variables are implemented internally at the stack level.

    • There is no default value for local variables here, so local variables should be declared and the initial values should be allocated before the first use.

For example:

Here, age is a local variable. This is the definition inside the pupage () method, whose scope is limited to that method.

public class test{public void Pupage () {int = 0;      Age = age + 7;   System.out.println ("Puppy is:" + age);      } public static void Main (String args[]) {test test = new test ();   Test.pupage (); }}

This will produce the following results:

Puppy Age Is:7
For example:

The following example uses age not to initialize it, so it will give an error message at compile time.

public class test{public void Pupage () {int age;      Age = age + 7;   System.out.println ("Puppy is:" + age);      } public static void Main (String args[]) {test test = new test ();   Test.pupage (); }}

Compile it, which will produce the following error:

Test.java:4:variable number might not has been initializedage = age + 7; ^1 Error
Instance variable:
  • An instance variable is declared in a class, but outside of the method, a constructor or any block.

  • When space is allocated to an object in the heap, the slots create values for each instance variable.

  • When an object is created with the use of the keyword "new", the object is destroyed destroying the created instance variable.

  • An instance variable is considered to be a value that must be referenced by more than one method, a constructor or block, or a critical part of an object's state must appear in the entire class.

  • Instance variables can be declared at levels before or after use.

  • An access modifier can give an instance variable.

  • Instance variables for all methods, constructors and blocks are visible in the class. Generally, it is recommended to make these variables private (access layer). However visibility subclasses can give these variables with the use of access modifiers.

  • The instance variable has a default value. The default value for a number is 0, and the Boolean value is false and the object reference is null. Values can be assigned in declarations or constructors.

  • Instance variables can be accessed directly through the class that invokes the variable name. However, in static methods and different classes (when an instance variable is given access) the objectreference.variablenameshould be called with the fully qualified name.

Example:
import java.io.*;p ublic class employee{   // this instance  Variable is visible for any child class.   public string  name;      // salary  variable is visible in  Employee class only.   private double salary;       // The name variable is assigned in the constructor.     public Employee  (string empname) {      name  = empName;   }   // The salary variable is  Assigned a value.   public void setsalary (Double empSal) {       salary = empsal;   }      //  this method pRints the employee details.   public void printemp () {       system.out.println ("name  : "  + name );       system.out.println ("SALARY :"  + salary);   }    public static void main (string args[]) {      employee  empone = new employee ("Ransika");       empone.setsalary (       empone.printemp ();    }}

This will produce the following results:

name:ransikasalary:1000.0
class/Static variables:
  • A class variable is also known as a static variable declared in the Static keyword of the class, but outside of the method, a constructor or a block.

  • Each class variable has only one copy, regardless of how many objects are created from it.

  • Static variables are seldom used and are not declared as constants. Constants are declared as public/private, final and static variables. Constant variables never change from their initial values.

  • Static variables are stored in static memory. It is rare to use static variables other than declared as final, as a public or private constant.

  • Static variables are created when the program starts, and the program stops destroying.

  • Visibility is similar to instance variables. However, most static variables are declared public because they must be available to the consumer of the class.

  • The default value is the same instance variable. For a number, the default value is 0, the Boolean value, which is false, and the object reference, which is null. Values can be assigned in declarations or constructors. Additional values can be assigned in a special static initialization block.

  • Static variables can be accessed by invoking the class name. classname.variablename.

  • When a variable is defined as public static final , the name (constant) of the variable is uppercase. If the static variable is not exposed and the final naming syntax is the same as the instance variable and the local variable.

Example:

Import java.io.*;p ublic class employee{//Salary variable is a private static variable private static double salary;   DEPARTMENT is a constant public static final String DEPARTMENT = "development";      public static void Main (String args[]) {salary = 1000;   System.out.println (department+ "average salary:" +salary); }}

This will produce the following results:

Development average salary:1000

Note: If a variable is accessed from an external class, the constant should be accessed employee.department


This article is from the "Listen to the Rain Hope Eternal" blog, please be sure to keep this source http://yongguang.blog.51cto.com/9153118/1686870

Java Variable type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.