"Learning Notes" Java Object-oriented programming 3

Source: Internet
Author: User

I. Object block

The code is as follows:

1  Public classTestobject {2     inti;3     intJ;4     {5System.out.println ("Object block execution ...."));6          This. i = 1;7          This. J = 99;8     }9     Ten      PublicTestobject () { OneSystem.out.println ("Constructor performs ..."); ASystem.out.println ("i =" +i); -System.out.println ("j =" +j); -     } the      -      Public Static voidMain (string[] args) { -Testobject test =NewTestobject (); -          +     } -}

Execution Result:

= 1= 99

As in the above code, in the 4~8 line, the object block.
As can be seen from the execution results, the object block is better than the constructor, used to initialize the member variables, and through this can be seen, when the object is created, the object block is executed, visible, object block belongs to the object.

Two. Static block

The code is as follows:

1  Public class Testobject {2     Static {3         System.out.println ("Static block is loaded ..."); 4     }5     6public     staticvoid  Main (string[] args) {7         8    }9 }

Operation Result:

Static blocks are loaded ...

Why is the main function empty and can show this result? Because the main function is a main entry, and the portal executes before the class is loaded, and the static block belongs to the class, the static block is executed when the class is loaded.
Static blocks are typically executed only once.

Benefit usage: For some common initialization things, initialize the program at the beginning of the procedure. (Takes advantage of a feature that executes only once and is superior to the main function)

Three. Package Packages

Each class is placed in a package and is regulated. The name of the package is recommended for use with company names. For example, google.com can name the package com.google. Project name. Module Name

Packages are strictly categorized according to the system, which is very important, that is, the package is a folder, the first level of such a continuation, the final sub-file is a class file.

Package import, with the import package name;

Note: When calling each other between the packages and the classes in the package, use import imports, inheriting a class from another package, and so on.

A cat class inherits the animal class. Notice that there is an import in front of the class that imports the animal's package. As follows:

 Package Com.abc.one; Import Com.abc.two.Animal;  Public class extends Animal {    static{        System.out.println ("A cat");    }          Public Static void Main (string[] args) {        new  Cat ();        Cat.mathod ();    }}

Animal type:

 Package Com.abc.two;  Public class Animal {    publicstaticvoid  main (string[] args) {        System.out.println ("Animal Class");    }      Public void Mathod () {        System.out.println ("Animal Method");}    }

The result of running the Cat.java file is:

A Cat Animal Method

The creation of the package:

Two calls between different packages are implemented using Import package imports.

Four. Access control

 Package Com.abc.one;  Public class Test {    privateint  i;     int J;     protected int N;      Public int m;}
 Package Com.abc.two; Import com.abc.one.Test;  Public class Testsmall {    publicstaticvoid  main () {        new Test ();        System.out.println (TEST.I);    }}

Unable to run because I private type.

"Learning Notes" Java Object-oriented programming 3

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.