美文网首页
Java_basic_3: static, this 关键字

Java_basic_3: static, this 关键字

作者: DiscoSOS | 来源:发表于2017-10-07 23:48 被阅读0次

static

  • static 静态变量
  • Java 中被 static 修饰的成员称为静态成员或类成员。它属于整个类所有,而不是某个对象所有,即被类中的所有对象所共享。
  • 静态成员可以1.使用类名直接访问,2.也可以使用对象名进行访问。当然,鉴于他作用的特殊性更推荐用类名访问~~
  • static 可以修饰变量、方法 和代码块
image.png

static 修饰方法

  • main 方法就是一种静态方法
public static void main(String args[]){
//
}
  • 通过类名直接调用static 方法(推荐)
  • 通过对象调用static方法
package com.chee3e;

public class HelloWorld {
    public static void print() {
        System.out.println("Hello!");
    }
    
    public static void main(String args[]) {
        //1.
        HelloWorld.print();
        //2.
        HelloWorld demo = new HelloWorld();
        demo.print();
    }

}

  • 静态方法中 可以使用同一类中的静态变量,但不能调用非静态变量
public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    
    // 
    public static void print() {
        System.out.println("Welcome" + Name); //error!!
        System.out.println("Hobby is :" + hobby );
    }
}

  • 如果想要在静态方法中访问类中的非静态变量,可以先创建类的对象,然后通过这个对象来访问非静态变量

public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public static void print() {

        HelloWorld demo = new HelloWorld();
        System.out.println("Welcome to EU, " + demo.name);
        
        System.out.println("Hobby is :" + hobby );
    }
    
    public static void main(String args[]) {
        print();
    }

}

  • 在普通方法中则可以直接访问同类的非静态变量和静态变量
public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public  void show() {
        System.out.println("WELCOME TO EU, " + name);
        System.out.println("His hobby is " + hobby);
    }
}   
  • 但是在静态方法中,比如main 方法中, 如果要使用普通方法,则需要先创建对象,再调用
public class HelloWorld {
    String name = "Jamie";
    static String hobby = "football";
    // 
    public  void show() {
        System.out.println("WELCOME TO EU, " + name);
        System.out.println("His hobby is " + hobby);
    }
    
    public static void main(String args[]) {
        // create a new object
        HelloWorld demo = new HelloWorld();
        demo.show();
        
    }
}

初始化块/static初始化块 initializer block/static initializer block

public class HelloWorld {
    int num1;
    int num2;
    static int num3;
    
    // constructor
    public HelloWorld(){
        num1 = 91;
        System.out.println("use constructor to assign the variable num1" );
        
    }
    
    // initializer block
    {
        num2 =74;
        System.out.println("use an initializer block to assign the variables num2");
    }
    // a static initializer block
    static {
        num3 = 99;
        System.out.println("use a static initializer block to assign the variables num3");
    }
    
    
    public static void main (String args[]) {
        HelloWorld demo = new HelloWorld();
        System.out.println(demo.num1);
        System.out.println(demo.num2);
        System.out.println(num3);
        //
        HelloWorld demo2 = new HelloWorld();    
    }

}
  • 观察代码,分别使用构造方法,初始化块,static初始化块, 给三个变量赋值, 其中,static初始化块只能给静态变量赋值

*运行代码结果:

use a static initializer block to assign the variables num3
use an initializer block to assign the variables num2
use a constructor to assign the variable num1
91
74
99
use an initializer block to assign the variables num2
use a constructor to assign the variable num1
  • 从代码运行的顺序可以看出, 程序会优先使用 静态初始化代码块(static initializer block ),且只会执行一次(由于静态初始化块只在类加载时执行一次,所以当再次创建对象 demo2 时并未执行静态初始化块)。
  • 接着会执行普通初始化代码块,最后才是构造方法。

this 关键字

  • this 关键字代表当前对象
  • this.属性 ----->操作当前对象的属性
  • this.方法 ----->操作当前对象的方法
  • 在封装对象的属性的时候会经常用到this 关键字
public class Telephone {
    //
    private double screen;
    private double cpu;
    private double mem;
    
    public void sendMessage() {
        System.out.println("sendMessage");
    }
    
    public double getCpu() {
        this.sendMessage();
        return cpu;
    }
    public void setCpu(double cpu) {
        this.cpu = cpu;
    }
    public double getMem() {
        return mem;
    }
    public void setMem(double mem) {
        this.mem = mem;
    }
}

相关文章

  • Java_basic_3: static, this 关键字

    static static 静态变量 Java 中被 static 修饰的成员称为静态成员或类成员。它属于整个类所...

  • OC中static、const、extern关键字理解

    static关键字 static关键字用于修饰变量。 static修饰局部变量当使用static修饰局部变量时, ...

  • static关键字

    Static关键字概述 static(静态)关键字可以用于修饰变量、方法和代码块。我认为static关键字的主要用...

  • 【Java】关键字

    一、 static关键字 static关键字的总结: static关键字 可以再没有创建对象的时候进行调用类的元素...

  • Java学习Day03

    今日学习内容总结 Static关键字 Arrays类 Math类 继承 Static关键字 一旦用了static,...

  • java基础-day11-static关键字

    static关键字和接口 1. static关键字【重点】 1.1 static修饰静态成员变量 1.1.1 为什...

  • C语言中的static关键字

    @TOC C语言中的static关键字 static 关键字,意为静态。 static 变量 特性:全局变量在函数...

  • Static关键字

    C语言中的static关键字和Java的static关键字意义不一样。 1 用static修饰函数 static用...

  • C基础知识

    基础问题 1.static关键字和const关键字。 static: 作用于变量时:用static声明局部变量--...

  • 13.面向对象进阶—静态关键字static

    面向对象进阶 静态关键字static static关键字的作用,修饰成员变量的用法 static是静态的意思,可以...

网友评论

      本文标题:Java_basic_3: static, this 关键字

      本文链接:https://www.haomeiwen.com/subject/kwklyxtx.html