-java Basics-Objects

Source: Internet
Author: User

1. Creating Objects (Instances)

The New keyword since the class is the template for the object, the new class name (); is the meaning of creating an object. New creates an object one time, creating an object in other words, creating the memory again. New creates several objects on several occasions. For example://This is a student class public classes Student {String name; String address; int age; String sex; void Dushu () {System.out.println ("reading ...");} void Lianxi () {System.out.println ("practicing ...");} Here we new the object of this class of students, and give this object to the variable Stu, in the object of the variables attached to the parameter. Then call the method of this object. public class Test {public static void main (string[] args) {Student stu=new Student (); stu.sex= "male"; Stu.name= "Zhang San"; Stu.add Ress= "Hunan"; stu.age=12; Stu.dushu (); Stu.lianxi (); } }

2. Static members? Instance member?

1. Instance members

An instance member is a new object, so variables and methods are called object members or instance members.

2. Static members

Static members are properties and methods that add static to the class template. A static member has only one copy of memory, even if the object is simply referencing the static member of the class in memory, the object does not create its own registry memory to store but to reference. So static member object instances of the properties and methods that have the static keyword are not going to register memory but simply refer back to the static members of the class. Only instance member objects without static will go to register memory themselves. So static members can be accessed directly from the class name without the new object. Because this class is loaded, it already has a static member. For example:

Static members can be accessed directly without the new object.

3. Reference delivery

Since a static member is a shared copy of all instances. and instance members are "learning Java, to the Kaige123.com school" each have their own share. Then there is the relationship of reference passing.

1. Static members will change all instances as long as one instance modifies it. 2. Instance members that's the one you're modifying yourself to have no effect on other instances.

-java Basics-Objects

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.