Java code block, object array

Source: Internet
Author: User

Package com.cloud.day1;

Public class Demo1 {

Public static void main (string[] args) {

//Define a common block of code

{

int x=20;

System. out. println (" code block:x="+x);

}

int x=200; // Global Variables

System. out. println ("x="+x);

System. out. println ("---------------");

//Construction code block use: Instantiation will execute building block content

New Demo2 ();

New Demo2 ();

New Demo2 ();

System. out. println ("---------------");

//Static code block use: Execute first, and execute once

New Demo3 ();

New Demo3 ();

New Demo3 ();

System. out. println ("---------------");

//Construction Method Privatization

demo4d4=null;

D4=demo4. d4; // accessing static properties in a class

D4.print ();

System. out. println ("---------------");

///static method gets an instance of the class

Demo5de1=demo5. GetDemo5 ();

Demo5de2=demo5. GetDemo5 ();

// This notation is also called a singleton design pattern, and the program actually instantiates only one object

System. out. println (de1+"---"+de2);

De1.print ();

De2.print ();

System. out. println ("---------------");

//object array use

demo6dem[]=newdemo6[3];

dem[0]=New Demo6 ("tom1");

dem[1]=New Demo6 ("Tom2");

dem[2]=New Demo6 ("Tom3");

for (int i=0;i<dem.length; i++) {

System. out. Print (Dem[i].getname () +",");

}

System. out. println ();

System. out. println ("---------------");

}

}

class demo2{

{

System. out. println (" This is a building block ");

}

Public Demo2 () {

System. out. println (" This is the construction method ");

}

}

class demo3{

{

System. out. println (" This is a building block ");

}

Static {

System. out. println (" static code block ");

}

Public Demo3 () {

System. out. println (" This is the construction method ");

}

}

class demo4{

Static Demo4d4=new Demo4 ();

Private Demo4 () {

}

Public void print () {

System. out. println ("Hello,china");

}

}

class demo5{

privatestatic Demo5 d5=new Demo5 ();

Private Demo5 () {

}

Public static Demo5 GetDemo5 () {

return d5;

}

Public void print () {

System. out. println ("Hello,china");

}

}

class demo6{

Private Stringname;

Public Demo6 (String name) {

this. name=name;

}

Public String GetName () {

returnthis. name;

}

}

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Java code block, object array

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.