Several types of Java variables

Source: Internet
Author: User

Local variables:

Age is a local variable. Defined in the Pubage () method, its scope is limited to this method.

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

Member variables:

Import java.io.*;p Ublic class employee{//This member variable is visible to the public String name;//private variable, only in the class visible private double salary;// Assign a value of public Employee (String empname) {name = EmpName;} to name in the constructor Set the value of salary public void setsalary (double empsal) {salary = Empsal;} Print information 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 (1000); Empone.printemp ();}}
Running Result: Name:ransika
salary:1000.0

class variables (static variables):

Import java.io.*;p Ublic class employee1{//salary is a static private variable that is a constant public static double salary;//department Final String departement = "Development";p ublic static void Main (String args[]) {salary = 1000; System.out.println (departement+ "average salary:" +salary);}}

Operation Result:

Development Average Salary:  

Problem encountered, Java compilation error: Parsing has reached the end of the file?

Resolution: Missing curly braces, search carefully!

Several types of Java variables

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.