Java Classroom Validation

Source: Internet
Author: User
Tags instance method stub

one, Why the following code cannot be compiled

 public class Test1 {  publicstaticvoid  main (string[] Args) {  //  TODO auto-  Generated method stub foo obj1=new  foo ();    }}class  Foo {int  value;    public Foo (int  initvalue) {value=initvalue; }}

Error: only one constructor with parameters is Defined. The Foo class object defined in the main function calls the parameterless Constructor.

Correct:

 public  class   Test { public  static  void   main (string[] Args) { //  TODO auto-generated method stub  int  x=0;    Foo obj  =new   Foo (x); }}  class   foo{ int   v;  public  Foo (int   x) {v  =x; }}

When there is an inheritance relationship between classes, creating a subclass object causes the parent class to initialize the execution of the BLOCK.

third, Run the Teststaticinitializeblock.java example, observe the output, and summarize the "order of execution of static initialization blocks".
Summary: Object Initialization Order: first executes the parent class static content, after the parent class static content executes, then executes the subclass the static content, when the subclass static content executes, then goes to see the parent class has the Non-static code block, if has executes the parent class Non-static code block, the parent Class's non-static code block executes completes , then executes the parent Class's construction method, and after the parent Class's construction method executes, It goes on to see if the subclass has no non-static code block, and if there is one, executes the Subclass's non-static code BLOCK. The non-static code block of the subclass executes and then executes the Subclass's construction method. In short, the static code block content executes first, then executes the parent class Non-static code block and construction method, then executes the subclass non-static code block and constructs the Method.

four, static methods only allow access to static data, then, How to access the instance members of the class in a static method (that is, A field or method that does not have the static keyword attached)?

A static method or property in a class is not a member of the class in nature, and when the Java Virtual machine is loaded in the class, the static objects already have the object, which is simply "resident" in the class and does not need to be instantiated by the Class's constructor (constructor) class, rather than by static properties or METHODS. When a class is loaded, it does not exist, and it is necessary to have an instance object of the class that is dependent upon the constructor of the class being Executed.

When you call a static method externally, you can use the "class Name. method name" way, or you can use the "object Name. method name" way. The instance method is only in the following way. That is, calling a static method eliminates the need to create an object.

When a static method accesses members of this class, it only allows access to static members (that is, static member variables and static methods), not to instance member variables and instance methods, and the instance method does Not.

five, after-school assignments: using static fields and constructors of classes, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。

     public classTest { public Static voidmain (string[] Args) {//TODO auto-generated method stubs//calledSum one=NewSum (2); Sum=NewSum (3);  System.out.println (one.get ()); }}       public classsum{ public Static intA=0; intx;  publicSumintY) {x=y; A++; } public intget () {returna; }      }

Java Classroom Validation

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.