JavaBean constructor method This static keyword

Source: Internet
Author: User

Features of JavaBean:
? Private member properties
? Public Member Method
? There are get/set methods for assigning/taking values to member variables
? Empty construction Method
JavaBean that meet these conditions are called standard.


Construction Method
[modifier] Constructor Method name (formal parameter list) {
//0 to multiple executable statements to form the execution body of the construction method
}
class student{
Public Student () {
System.out.println ("This is the method of constructing the students ' class of NULL parameters");
}
}
class person{
private int id;
Public Person () {}//Null parameter construction method
Public person (int a) {///overloaded constructor method, with an int type argument
id = A;//assign a value to an instance variable in a construction method
System.out.println ("This is a construction method with parameters");
}
Public person (int a, int b) {
System.out.println ("This is a construction method with two parameters");
}
}
Considerations When constructing method definitions:
1. The construction method must have the same name as the class
2. The constructor does not return a value or void, and once the return value or void is added, the method is a member method, not a constructor.
class demo{
//public Demo () {} If no construction method is defined, the system automatically adds a non-parametric construction method
Public Void Show () {
System.out.println ("Hello World");
}
}
class demo{
Public Demo (int id) {
System.out.println ("This is a construction method with parameters");
}
}
//Define an employee class, analyze several members yourself, and then give member variables, construction methods, GetXxx ()/setxxx () methods,
//And a method for displaying all member information. and tested.
class employee{
private String name;
private String employeeId;
private int age;
//Construction method
Public Employee () {}
//getxxx ()/setxxx () method
Public String Getemployeeid () {
return Empoloyeeid;
}
Public void Setemployeeid (String employeeId) {
This.employeeid=empoloyeeid;
}
Public String GetName () {
return getName;
}
Public void SetName (String name) {
this.setname= name;
}
public int Getage () {
return age;
}
Public void Setage (int.) {
this.age = age;
}
Public Void Show () {
System.out.println ("Employee code is:" +getemployeeid + "name is:" +getname+ "Age is:" +getage ");
}
}
class employeetest{
Public static void Main (string[] args) {
//Create Objects
Employee E = new Employee ();
//Assign a value to a member variable
E.steemployeeid ("1234");
e.stename ("John Doe");
E.setage (+);
e.show ();
}
}
//define an arithmetic class MyMath, provide basic subtraction functionality, and then test.
Import java.util.scanner{
class mymath{
public int Add (int a, int b) {
return a+b;
}
Public int sub (int a, int b) {
return a-B;
}
public int num (int a, int b) {
return a*b;
}
public int div (int a, int b) {
return a/b;
}
}
//Test class
class mymathtest{
Scanner s = new Scanner (system.in);
System.out.println ("Enter the first number");
int a = S.nestint ();
System.out.println ("Enter a second number");
int b = S.testint ();
mymath mm = new MyMath ();
System.out.println (Mm.add (A, b));
System.out.println (Mm.sub (A, b));
System.out.println (Mm.mul (A, b));
System.out.println (Mm.div (A, b));
}
}



class person{
Public Person () {}
Public void Sayhai () {
System.out.println ("Hello");
}
Public void Speak (String content) {
//person P = new person ();
//P.sayhi ();
This.syahi ();//sayhi ();//Another method that calls this object can omit the This keyword
System.out.println ("I Want to say:" +content);
}
}

Class persontext{
public static void Main (string[] args) {
Person pp = new person ();
Pp.speak ();
}
}

//this keyword in the constructor method, initializes the member variable
class person{
private String name;
private int id;
private String gender;
Public Person () {}//NULL parameter construction method
//constructor with three parameters, because the parameter and member variable have the same name, the This keyword distinguishes
Public person (String name, int id,string gender) {
This.name=name;
this.id = ID;
this.gender = gander;
}
Public person (String name,int id) {
this.name = name;
this.id = ID;
}
}
The //this keyword is used to refer to the current object, and the This keyword can be used in construction methods
//*********************************************
member variable/member method with static modifier, called class variable/class method,
member variable/member method without static modifier is called instance variable/instance method

>>>>>>>>>>> static only access to static
class teacher{
public int num=10;
public static int num2 =;
Public Void Show () {
System.out.println (num);//implicitly tells you to access a member variable
System.out.println (This.num)//explicitly tells you that access is a member variable
}
}

static methods can be accessed by:
1. Static member Variables
2. Static member Methods
non-static methods can be accessed by:
1. Static/non-static member variables
2. Static/non-static member methods
to summarize, simply remember:
Static only access static, non-static two can access

The object initialization process consists of two aspects:
Construction of code block execution + construction method execution

Constructing blocks of code: Extracting common statements from the construction method and executing them before the construction method executes
Static code block: executed once at class load to initialize class (member variable)

JavaBean constructor method This static keyword

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.