3 minutes to figure out the three major modifiers of Java

Source: Internet
Author: User
Tags modifiers

3 minutes to figure out the three major modifiers of Java


Java three decorations: Static,final,abstract, in the Java language everywhere, but they can modify what components, the meaning of the cosmetic component and what limitations, always confused. So to summarize the comparison.


1 static modifier


The static modifier is capable of decorating the property, method, and initial code block. Local variables and classes cannot be decorated.

The first thing to understand is that attributes are static and non-static, statically called static properties, also called class variables, non-static, called instance variables.

Static variables and static methods are collectively referred to as static members.


Modifier properties

A static property is also called a class variable, which is not part of an object, belongs to a class, and is shared by all objects.

Eg: class name. static Property = Object 1. Static Property = Object 2. Static property = Address in the same piece of memory

Modification methods

1: Static methods can be called directly by the class name. Method Name.

2: Static methods can only: Access static members, cannot access non-static members.

3: The This keyword cannot be used in static methods.

4: Static method is not polymorphic, can only be overridden by static methods.


Modifying an initial block of code

The static adornment of the initial block of code is called a block. A static block of code is run when the class loads. Here's a recap of class loading.



Class loading:

Testperson. java

Class person{

static{

System.out.println ("Static Here");

}

Public person () {

System.out.println ("construction method");

}

}

public class Testperson () {

public static void Main () {

person P1 = new person ();

person P2 = new person ();

}

}

Running the Testperson. java file compiles the generated Testperson. Class and Person.class two class files.

Executing Java Testperson First starts the Java Virtual Machine (JVM), then finds the Testpeson.calss file on the hard disk and begins interpreting execution,

At this point the JVM only testperson.class the information of this class, sequentially executes to the main method, encounters, person p1 = new person (),

Create a Person object, the JVM will find the Person.class file according to the CLASSPATH environment variable, and read the information of the person class into the JVM.

Save them. This process is class loading.

Class Loading: loading class information;

Execute static code block;

Allocates space for static properties and initializes values.

The execution order of the above example is as follows:

static here

Construction method

Construction method


2final modifier

The final modifier can modify variables, methods, and classes.



Final modifier variable

The final modified variable is called a constant, and once the assignment cannot be changed. For a base type, its value cannot be changed, and its address cannot be changed for the object type.

Assignment of the final variable description: A generic instance variable is assigned an empty create default value when it is created. But the final variable can only have one assignment at a time, so for the final variable, the JVM does not assign it a value, and the only assignment is left to the programmer.

An instance variable of the final type is assigned a value of 1: Initialize the property,

2: Call the constructor method, two ways must seize one opportunity, but cannot try to seize two kinds of opportunities.

Final Modification method

The final decoration method, which indicates that the method cannot be overridden by a quilt class.

Eg:class a{Public final void A1 () {...}}

Class B extends a{public void A1 (...)}

Compilation errors cannot be overwritten

Final Modifier class

The final decorated class indicates that the class cannot be inherited

There is a final method in a class that can be inherited, but cannot overwrite the final method, but a class is the final class, and the class cannot be inherited, much less about overrides.


Eg:final Assignment Value

Class Person {

final int num = 10; Initialize Properties

public perosn (int num) {

This.num = num;

}

}

Class Person {

final int num;

public perosn (int num) {//Constructor method assignment

This.num = num;

}

}

Class Person {

final int num = 10; Errors, both of which are caught in both ways

public perosn (int num) {//error, both of which are caught

This.num = num;

}

}



3 abstract modifier

Abstract can modify classes and methods,


Abstract Modifier class

The abstract modifier class is called an abstraction class. An abstract class can only be used to declare that a reference cannot be used to create an object. In a sense, abstract class is to let subclass inherit, abstract class declaration reference, let this reference point to subclass. Subclasses to implement abstract methods.

Abstract classes can have non-abstract methods, but an abstract method in a class must be an abstract class. Subclasses inherit abstract classes either by themselves as abstract classes or by implementing all abstract methods in the parent abstract class.

Abstract Modification Method

An abstract modification method, called an abstraction method. An abstract method means that only a method that declares that there is no implementation.

public abstract void A1 ();//abstract method.

public void A1 () {};//NULL implementation method


Abstract class a{

public void A1 () {}//Abstract class can have non-abstract methods

public abstract void A2 ();//But with abstract methods it must be an abstract class.

}

Class B extends a{

public void A2 () {

System.out.print ("Subclasses are either abstract classes or implement all abstract methods of the parent abstract class");

}

}


public class testa{

public static void Main () {

A;//correct, abstract classes can be used to reference

A = new A ();//error, A abstract class, abstract class cannot be used to create an object

A = new B ();//correct

A.A2 ();//correct

}

}


The most important function of an abstract class is to define a function that implements a subclass.

It fully embodies the common denominator in the parent class, the basic principle. Abstract and polymorphism are closely related, and its ideas can be referred to Java three major features.




Limited to the length of the article, here is just a tip. Due to the author's limited level, writing time is also very hasty, the text will inevitably appear some errors or inaccurate places, inappropriate to ask readers to criticize correct.

The above content wants to be helpful to everybody. Welcome to discuss and make progress together.



This article from "The Heart has the tiger, the fine smell Rose" the blog, please must keep this source http://zhangdongxu.blog.51cto.com/12029530/1920716

3 minutes to figure out the three major modifiers of Java

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.