Java Foundation Chapter eighth (invocation of static members, encapsulation)

Source: Internet
Author: User

One, the static member of the two kinds of calls.

1. Object. member variable = value;

Example: STU1. name = "Java";

2. Class name. Member mode; It can be called directly in this way without the need for new, and the Calling method can also (Student. Getnum ())

Example: Student. count++;

Note:

Static methods can only access static variables, and static can only access statics.

Non-static methods can access both static and non-static variables.

Example 1:class student{

static int count;

public int Getnum () {///non-static method can call a static variable or call a non-static variable

count++;

return count;

}

}

The main program call must be new to an object before it can be called (because the Getnum () method is non-static, so it is not shared, to be new out of the object, it can be called with an object)

Student stu1 = new Student ();

int count1 = Stu1.getnum ();

Example 2:class student{

static int count;

public static int Getnum () {

count++;

return count;

}

}

The main program calls int count2 = Student.getnum ();

Second, static block.

static{

}

Static blocks can only be loaded and run once, and executed first.

Example: Class student{

public static int count;

static{

count++;

}

public void Studentnum () {

System.out.println (count);

}

}

Main program Call: New Student (). Studentnum (); 1

New Student (). Studentnum (); 1

Three, encapsulation.

1. Encapsulate the function of the code:

Provide only a name to the outside world to allow access to these properties or functions.

2. Example: Bank security

Class creditcard{

private double salary; Outside access to the salary, it encapsulates

Public double getsalary () {

return salary;

}

public void Setsalary (double sal) {

if (Sal <= 0) {

return 0.0;

}else{

salary = sal;

}

return salary;

}

}

public class money{

public static void Main (string[] args) {

CreditCard cc = new CreditCard ();

Cc.setsalary (100000000);

Double salary = cc.getsalary ();

}

}

Note: Private privately, only own, outside access.

Java Foundation Chapter eighth (invocation of static members, encapsulation)

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.