Common code block && Construction code block && static code block difference __ Common code block

Source: Internet
Author: User

public class Test1 {

private int A;
private static int C;
public static void Main (string[] args) {
 Test1 test1 = new Test1 (a);
 System.out.println (c);       Private members of this class can be called directly
 //system.out.println (a);     No, because it is a static function, you cannot directly access non-static properties      
 System.out.println (test1.a);//static functions can indirectly access static properties} public Test1 by reference to a
class
(int A, int c) {
    super ();
    THIS.A = A;
    THIS.C = C;
}

}
/*
* Private is a proprietary variable that can only be used in the current class, and the main method in the topic is also in the current class, so it is accessible without any details being investigated;
*/

Package thirdday;

public class Test2s extends test2f {

Public Test2s ()
{}
{
    System.out.println ("I ' M B class");
}
Static
{
    System.out.println ("Static B");
}

public static void Main (string[] args) {
    test2s test2s = new Test2s ();
}

}
Class test2f
{

Public test2f ()
{}
{
    System.out.println ("I ' M A class");
}
Static 
{
    System.out.println ("A static");
}

}
/*

A Static
Static B
I ' M A class
I ' M B class
*/

Common code blocks in Java, building code blocks, static code block differences, and code examples
Order of execution: (priority is from high to low.) The static code block >mian method > constructs the code block > constructs the method.
Where the static code block is executed only once. The construction code block executes every time the object is created.

Normal code block: the {} that appears in a method or statement is called a generic block of code. Common code blocks and general statement execution orders are determined by the order in which they appear in the code – "first appear first"
/*public class codeblock01{
3 public static void main (string[] args) {

         {
          int x=3;
          System.out.println ("1, variable in normal code block x=" +x);    
         }

         int x=1;
         SYSTEM.OUT.PRINTLN ("The variable in the Main method x=" +x);                          
         {
          int y=7;
          System.out.println ("2, variable in normal code block y=" +y);    
         }}
    

Run Result:
1, the normal code block within the variable x=3
Variable X=1 within the Main method
2, the normal code block within the variable y=7
*/
/**
*//Building BLOCK: A code block defined directly in the class and without the static keyword is called {} to construct the code block. The construction code block is invoked when the object is created, each time the object is created, and the execution order of the Construction code block takes precedence over the class constructor.

public class codeblock02{
{
System.out.println ("first code block");
}

Public CodeBlock02 () {
    System.out.println ("construct method");
    }

    {
      System.out.println ("second building Block");
    }
  public static void Main (string[] args) {
      new CodeBlock02 ();
      New CodeBlock02 ();
      New CodeBlock02 ();      
}

}
/*
Execution results:
First code block
Second building block
Construction method
First code block
Second building block
Construction method
First code block
Second building block
Construction method
*/
/*
*//Static code block: a block of code declared in Java using the static keyword. Static blocks are used to initialize the class and initialize the properties of the class. Each static code block is executed only once. Because the JVM executes a static code block when the class is loaded, the static code block is executed before the main method.
If a class contains more than one static block of code, then the code is executed first, followed by the defined code.
Note: 1 Static blocks of code cannot exist in any method body. 2 static code blocks cannot directly access static instance variables and instance methods, which need to be accessed through instance objects of the class.

Class code{
{
SYSTEM.OUT.PRINTLN ("Building Blocks of code");
}

static{
    System.out.println ("code's static Block");

Public code () {
    System.out.println ("Method of Code Construction");
    }

public class codeblock03{
{
System.out.println ("Tectonic block of CodeBlock03");
}

static{
    System.out.println ("Static code block for CodeBlock03");
    }

    Public CodeBlock03 () {
         System.out.println ("Constructed method of CodeBlock03");
        }

  public static void Main (string[] args) {
        System.out.println ("CodeBlock03 Main Method");
        New Code ();
        New Code ();
        New CodeBlock03 ();
        New CodeBlock03 ();
      }

/*
CODEBLOCK03 Static block of code
The Main method of CodeBlock03
Code blocks for static codes
Building blocks of code
The construction method of code
Building blocks of code
The construction method of code
Building blocks of CodeBlock03
The construction method of CodeBlock03
Building blocks of CodeBlock03
The construction method of CodeBlock03
*/

1 in Java, the () class provides location-local file systems, basic operations on files or directories and their properties (d) a fileinputstream B filereader C FileWriter D file
* Note: A is a byte read to the file provided by the file's character read C provides the file's character write D provides basic operations on the file, including deletion, file path, and so on. 2 Which of the following conditions can terminate the current thread's operation. A when a higher-priority thread enters ready state B throws an exception C when the thread calls sleep () D when creating a new thread
* Note: First of all, the problem requires "termination", meaning that the "end" of the thread, the pro, is the end; second, understand thread end three reasons: 1.run () method execution completes, thread ends normally 2. The thread throws an unhandled exception, or error 3. Call the Stop directly Method end thread (not recommended, easy to cause deadlock)

*3 below to analyze the above options: A When a high priority thread enters the ready state, he only has the higher probability to be able to grab the CPU the execution power, does not have to be able to grab the execution power, Aerror B throws an exception thread to be sure to terminate; C calls the sleep and wait functions to just hold the thread temporarily Stop running, not the so-called termination, that is, "hang out"; D when creating a new thread, the thread is also added to the queue to preempt the CPU but it is not clear whether the execution power can be grabbed;
4 Methods in the Object class: contains 9

Protected Object Clone ()
Creates and returns a copy of this object.
Boolean equals (Object obj)
Indicates whether another object is "equal" to this object.
protected void Finalize ()
This method is called by the object's garbage collector when the garbage collector determines that there are no more references to the object.
Class

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.