How do you use NULL parameter construction methods in Java to automatically generate objects of different names, what is the difference between using non-static properties and static properties, and why? How to understand the static keyword

Source: Internet
Author: User

Non-static properties are used when NULL parameter constructs automatically generate objects

Code:

 PackageCom.swift;//automatic generation of objects using the parameterless construction method, the serial number is continuously self-increasing Public classPerson {Private intCount//If you are using a non-static property when defining a class, the result is the same, which is 1. Because this count life cycle is short, only inside the object     Public intID;  PublicString name;  Public intAge ;  PublicString City;  PublicPerson () {Super(); Count++;  This. id=count;  This. Name= "noname-" +count;  This. age=20;  This. city= "Shu" +count; }     PublicPerson (intID, String name,intage,string City) {         This. id=ID;  This. name=name;  This. age=Age ;  This. city=City ; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     PublicString GetInfo () {return"The person was id=" + ID + ", name=" + name + ", age=" + Age + ", city=" +City ; }    }

Results:

Use static properties when NULL parameter constructs automatically generate objects

Code:

 PackageCom.swift;//automatic generation of objects using the parameterless construction method, the serial number is continuously self-increasing Public classPerson {Private Static intCount//If you are using static properties when defining a class, you will get a different result. Count life cycle long, same as class     Public intID;  PublicString name;  Public intAge ;  PublicString City;  PublicPerson () {Super(); Count++;  This. id=count;  This. Name= "NoName" +count;  This. age=20;  This. city= "Shu"; }     PublicPerson (intID, String name,intage,string City) {         This. id=ID;  This. name=name;  This. age=Age ;  This. city=City ; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString getcity () {returnCity ; }     Public voidsetcity (String city) { This. City =City ; }     PublicString GetInfo () {return"The person was id=" + ID + ", name=" + name + ", age=" + Age + ", city=" +City ; }    }

Results:

How do you use NULL parameter construction methods in Java to automatically generate objects of different names, what is the difference between using non-static properties and static properties, and why? How to understand the static keyword

Related Article

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.