java--Object-oriented advanced (final keyword, static keyword, anonymous object, inner class, package declaration and access, four access modifiers, code block)

Source: Internet
Author: User
Tags modifiers

First, final keywords

The advent of inheritance increases the reusability of code and facilitates development. However, there are problems, some classes do not want to be inherited after the description, or some of the methods in the class function is fixed, do not want subclasses to rewrite . But when the subclass inherits these special classes, it can rewrite the method, how to solve it?

To solve these problems, you need to use a keyword final,final meaning to be final and immutable . Final is a modifier that can be used to modify a class, a member of a class, and a local variable.

1. Decoration class
    • A class that is final decorated cannot be inherited, but can inherit other classes.
Class YY {}final class Fu extends yy{}//Can inherit Yy classes class Zi extends fu{}//cannot Inherit Fu class
2. Modification Method
    • The final decorated method cannot be overridden, but the parent class is not in the final decorated method, and the subclass is overwritten to final.
The class Fu {//Final modified method cannot be overridden, but can be inherited using public    final void Method1 () {} public    void Method2 () {}}class Zi extends Fu { Override Method2 method public final void Method2 () {}}
3. Modifier variables
    • Final-modified variables are called constants, and they can only be assigned once.
final int i = 20;i = 30; Assignment is an error, and the final modified variable can only be assigned once
4. Modifying reference types
    • The value of the variable of the reference type is the object address value, and the address value cannot be changed, but the value of the object property within the address can be modified.
Final person P = new person ();     person P2 = new person ();p = p2;  Final modified variable p, the recorded address value cannot be changed p.name = "Xiao Ming"; You can change the value of the Name property in the P object

P cannot be a different object, and the value of the name or age property in the P object can be changed.

5. Modify member Variables
    • Modify the member variable, you need to assign a value before creating the object, otherwise error. (When there is no explicit assignment, you need to assign a value to multiple constructor methods.) )
Class Demo {//Direct assignment final int m = 100;//final Decorated member variable, you need to assign a value before creating the object, otherwise error. final int n; Public Demo () {//can be assigned n = 2016 in the constructor method that is called when the object is created;}}
second, the static keyword

When you define a class, you have the appropriate properties and methods in the class. The properties and methods are called by creating this class of objects. When a method of an object is called, the method does not have access to the object's unique data, and the method creates the object somewhat superfluous. But do not create the object, the method can not be called, then you will think, then we could not create the object, we could call the method?

Yes, we can do that by using the static keyword. static modifier, which is typically used to decorate a member of a class.

1. Define static member variables and static methods
    • A member variable that is modified by static belongs to a class and does not belong to an object of this class. That is, when multiple objects access or modify a static decorated member variable, one of the objects modifies the value of the static member variable, and the value of the static member variable in the other object changes, that is, multiple objects share the same static member variable
Class Demo {public static int num = 100;} Class Test {public static void main (string[] args) {Demo D1 = new demo ();D Emo d2 = new Demo ();d 1.num = 200; System.out.println (D1.num); The result is 200system.out.println (d2.num); The result is 200}}
    • Members that are modified by static can and recommend direct access through the class name.

Class name. static member Variable name

Class name. Static member Method name (parameter)

The name of the object. Static member variable name------It is not recommended to use this method, a warning appears

The name of the object. Static member Method name (parameter)------This method is not recommended, a warning appears

Class Demo {//static member variable public static int num = 100;//static methods public static void method () {System.out.println ("static Methods");}} Class Test {public static void main (string[] args) {System.out.println (demo.num);D Emo.method ();}}
    • Static content takes precedence over the existence of objects, (static methods) can only access static, and cannot use This/super. Statically decorated content is stored in a static area.
Class Demo {//member variable public int num = 100;//static methods public static void method () {System.out.println (this.num);    This.num error; You cannot use This/super. }}
    • In the same class, a static member can only access static members
Class Demo {//member variable public int num = 100;//static member variable public static int count = 200;//static methods public static void method () {//system.ou T.PRINTLN (num); In a static method, you can only access static member variables or static member methods SYSTEM.OUT.PRINTLN (count);}}
    • The main method is a static method that simply executes the entry for the program, which does not belong to any one object and can be defined in any class.
2. Define Static variables

In development, we want to define a static constant in the class, usually using the public static final modified variable to complete the definition. At this point the variable name is in all uppercase, and multiple words are concatenated with underscores.

defining static variables in a class

Define the format:

Public static Final Data Type Variable name = value ;

Class Company {public static final String company_name = "xin brother Bang Bang da!" ";p ublic static Void Method () {System.out.println (" a Static Approach ");}}

When we want to use static members of a class, we do not need to create an object to access it directly using the class name.

System.out.println (company.company_name);     Print static constant Company.method ();     Call a static method
defines a static variable in an interface

each member variable in the interface is used by default Public static Final decoration.

The member variable in all interfaces is already a static constant, and the assignment must be displayed because the interface does not have a constructor method. can be accessed directly with the interface name.

Defined:

Interface Inter {public static final int COUNT = 100;}

Access:

Inter.count
third, anonymous objects

An anonymous object is a statement that creates an object, but does not assign an object address value to a variable.

Create a Normal object

Person p = new person ();

Create an anonymous object

New Person ();

Create anonymous objects directly using, without variable names.

new Person (). Eat ()//eat method is a non-named Person object was called.

An anonymous object can only be used once if its reference variable is not specified.

new Person (). Eat (); creates an anonymous object that invokes the Eat Method

new Person (). Eat (); want to call again Eat method to recreate an anonymous object

Function: the nickname object can be used as a method to receive parameters, method return values

Class Demo {public static person Getperson () {//Normal mode//person p = new Person ();//return p;//Anonymous object as method return value return new person (); }public static void Method (person p) {}}class Test {public static void main (string[] args) {//Call Getperson method, get a Person object pe Rson person = Demo.getperson ();//Call methods Method Demo.method (person);//The anonymous object is the parameter Demo.method received as a method (new person ());}}
Iv. Internal class

Public static Final Data Type Variable name = value ;

java--Object-oriented advanced (final keyword, static keyword, anonymous object, inner class, package declaration and access, four access modifiers, code block)

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.