Java-class and object, java object

Source: Internet
Author: User

Java-class and object, java object

  • Class and Object
  • Member variables and local variables

Telephone. java

package com.test;public class Telephone {    double screen;    double cpu;    double mem;    public void sendMessage() {        System.out.println("screen:" + screen + ";cup:" + cpu + ";mem:" + mem);    }}

Demo. java

package com.test;public class Demo7 {    public static void main(String[] args) {        Telephone phone = new Telephone();        phone.sendMessage();        phone.screen = 5.4;        phone.cpu = 1.2;        phone.mem = 6.0;        phone.sendMessage();    }}

Output result:

Screen: 0.0; cup: 0.0; mem: 0.0
Screen: 5.4; cup: 1.2; mem: 6.0

  • Constructor

Telephone. java

Package com. test; public class Telephone {double screen; double cpu; double mem; public Telephone () {System. out. println ("construction method without Parameters");} public Telephone (double newScreen, double newSpu, double newMem) {screen = newScreen; cpu = newSpu; mem = newMem; System. out. println ("constructor with Parameters");} public void sendMessage () {System. out. println ("screen:" + screen + "; cup:" + cpu + "; mem:" + mem );}}

Demo. java

package com.test;public class Demo {    public static void main(String[] args) {        Telephone phone = new Telephone();        phone.screen = 5.4;        phone.cpu = 1.2;        phone.mem = 6.0;        phone.sendMessage();                Telephone phone1 = new Telephone(5.0, 1.5, 4.0);        phone1.sendMessage();    }}

Running result:

Construction method without Parameters

Screen: 5.4; cup: 1.2; mem: 6.0

Construction Method with Parameters

Screen: 5.0; cup: 1.5; mem: 4.0

  • Static variables

Static modified members in Java are called static members or class members. It belongs to the whole class, rather than an object, that is, it is shared by all objects of the class. When the system uses this class for the first time, it will allocate memory space for it until the class is detached!

Static members can use class names for direct access or object names for access. Of course, in view of its special role, class name access is recommended.

  • Static Method

Static methods are called static methods or class methods.

Package com. test; public class Demo8 {public static String classroom = "Java class"; public String schoolName = ""; public static void getName () {System. out. println ("======= getName ======="); System. out. println (classroom); Demo8 demo = new Demo8 (); System. out. println (demo. schoolName);} public void func () {System. out. println ("======= func ======="); System. out. println (classroom); System. out. println (schoolName);} public static void main (String [] args) {System. out. println ("======= main ======="); System. out. println (Demo8.classroom); System. out. println (classroom); Demo8.getName (); getName (); Demo8 demo = new Demo8 (); demo. func ();}}

Execution result:

======== Main ======== Java class ====== getName ====== Java class Hope Primary School ==== ===== getName ======= Java class Hope Primary School ======= func ======= Java class Hope Primary School
  • Assign values to initialization Blocks

The class declaration can contain multiple initialization blocks. When a class instance is created, these code blocks are executed in sequence. Static initialization blocks are called static initialization blocks.

Note: The static initialization block is only executed when the class is loaded and will only be executed once. At the same time, the static initialization block can only assign values to the static variables and Cannot initialize common member variables.

  

Package com. test; public class Demo9 {int age; String name; static String sex; public Demo9 () {age = 12; System. out. println ("initialize age through constructor") ;}{ name = "java"; System. out. println ("initialize name through initialization block");} static {sex = "female"; System. out. println ("initialize sex through static initialization block");} public static void main (String [] args) {Demo9 demo = new Demo9 (); System. out. println ("Age" + demo. age + "name" + demo. name + "gender" + sex); Demo9 demo1 = new Demo9 ();}}

Execution result:

Initialize sex through static initialization block initialize name through initialization block initialize age 12 name java gender female initialize name through initialization block initialize age through Constructors

When the program is running, the static initialization block is first executed, then the common initialization block is executed, and then the constructor is executed. Because the static initialization block is only executed once during class loading, no static initialization block is executed when the object demo2 is created.

  

 

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.