Java: Constructors, static (static)

Construction method

Constructor: create an object, the object is initialized with the members of
the format:
the same class name and method name 1.
2. No return type, not even void
3. No specific return value

 public Student() {}

Constructor overloads
1. If we do not give construction method, the system will automatically provide a constructor parameter no.
2. If we have given constructor parameters, the system will no longer provide a default constructor with no arguments.
Note: This time, if we want to use the no-argument constructor, you must give yourself. Recommend never give their no-argument constructor

public Student(String name, int age, String number) {
    this.name = name;
    this.age = age;
    this.number = number;
}
/*
因为已经给出了含有三个参数的构造方法
public Student(){}这个默认构造方法就不存在了
Student s=new Student();这句话将无法执行,因为无法创建无参的对象
所以我们要自己给出无参构造方法
*/
public Student() {}
public Student(String name, int age, String number) 
{
    this.name = name;
    this.age = age;
    this.number = number;
}

static (static)

If we have a shared data, such as citizenship, in a server in the country or region, nationality is the same
if each object are stored in a memory nationality is accounted for, so we can extract the nationality of this property, it only keep a copy and share it, then we should use the static keyword
is stored modified static member variables or member method in a static area method area of
static is a modifier for modifying member (member variables, member function).
When the members are static modification, the more a call, except that the object can be called, but also directly call the class name (class name. Static members)

class Person
{
	static String country="CN";
}
class  PersonDemo
{
	public static void main(String[] args) 
	{
	   Person p=new Person();
	   System.out.println(p.country);          //这两句的输出结果都是"CN"
	   System.out.println(Person.country);
	}

With specific content object is stored, the shared data can be modified static
method area (shared area, the data area): Method storage class and shared data

static features:

1. With the load and load class when the class is loaded into memory, static members and values are also loaded into memory, that is, static will disappear with the disappearance of the class, his longest life cycle
2. priority over object exists
3 is shared by all objects
4. the class name can be called
statically modified member variable is called a static member variables or call the class variable
has not been modified static member variables are called member variables or instance variables called

The difference between the instance variables and class variables

1. The storage position
class variables as the type present in the loading zone method
instance variables with the establishment of an object present in the heap memory
2. lifecycle
class variables longest life cycle, with the disappearance of the disappearance of the class
instance variable lifecycle with the disappearance of the object disappear

Note the use of static

1. Static methods can access only static members, non-static method can access both static can also visit non-static
non-static member with the creation of an object exists, static methods in preference to non-static existence, so can not call the method does not exist and variable. When the presence of non-static, static already exists
2. not a static method defined in this, super keywords
as preference to static objects exist, the process may not static this keyword appears
advantages:
a shared data object 1 is separate storage space, saving heap memory space, no need to each object are stored in a data
2. You can directly call the class name
drawbacks:
1. the life cycle is too long, take up memory
2. access appears limitations, only static accessing static

Static code block
static
{
	要执行的代码;
}
//随着类的加载而执行,只执行一次,切优先于主函数,用于给类进行初始化
class StaticCode
{
	static
	{
    	System.out.println('a');
	}
}
class StaticCodeDemo
{
	public static void main(String[] args) 
	{
		new StaticCode();        //执行该语句打印a
		new StaticCode();        //因为类已经存在所以该语句不执行
	}
}
class StaticCode
{
    StaticCode()
   {
       System.out.println('b');        //构造函数
   }
   static
   {
       System.out.println('a');         //静态代码块
   }
   {
       System.out.println('c');         //代码块
   }
    StaticCode(int x)
   {
       System.out.println('d');         //构造函数
   }
}
class StaticCodeDemo
{
   public static void main(String[] args) 
   {
		new StaticCode4);              
   }                                                             
}
/*
打印结果:
a
c
d
*/
/*
初始化时,先加载类,静态代码块随着类的加载而执行,
之后代码块随着对象的建立而被执行,最后执行对应的构造函数
*/
                 
                  

对象的初始化过程
Person p=new Person"小明"31);
步骤:
1.因为new用到了Person类,所以会先找到Person.class文件并加载到内存中
2.执行该类中的静态代码块(如果有的话),给Person类进行初始化
3.在堆内存中开辟空间,分配内存地址
4.在堆内存中建立对象的特有属性,并进行默认初始化
5.对属性进行显示初始化
6.对对象进行构造代码块初始化
7.对对象进行对应构造函数的初始化
8.将内存地址赋值给栈内存中的p变量
发布了26 篇原创文章 · 获赞 1 · 访问量 369

Guess you like

Origin blog.csdn.net/weixin_45919908/article/details/103468585