网站下一步工作怎么做怎么做网站图片的切换图
局部变量和成员变量的区别
区别1:代码中位置不同
成员变量:类中方法外定义的变量
局部变量:方法中定义的变量 代码块中定义的变量
区别2:代码的作用范围
成员变量:当前类的很多方法
局部变量:当前一个方法(当前代码块)
区别3:是否有默认值
成员变量:有
局部变量:没有
引用数据类型: null
区别4:是否要初始化
成员变量:不需要,不建议初始化,后续使用的时候再赋值即可
局部变量:一定需要,不然直接使用的时候报错
区别5:内存中位置不同
成员变量:堆内存
局部变量:栈内存
区别6:作用时间不同
成员变量:当前对象从创建到销毁
局部变量:当前方法从开始执行到执行完毕
1.package com.msb;
2.
3./**
4. * @Auther: msb-zhaoss
5. */
6.public class Student {
7.    byte e;
8.    short s;
9.    int c ;//成员变量:在类中方法外
10.    long num2;
11.    float f ;
12.    double d;
13.    char ch;
14.    boolean bo;
15.    String name;
16.    public void study(){
17.        int num = 10 ; //局部变量:在方法中
18.        System.out.println(num);//10
19.        //int num ;重复命名,出错了
20.        {
21.            int a;//局部变量:在代码块中
22.        }
23.        int a;
24.        if(1==3){
25.            int b;
26.        }
27.        System.out.println(c);
28.    }
29.    public void eat(){
30.        System.out.println(c);
31.    }
32.
33.    //这是一个main方法,是程序的入口:
34.    public static void main(String[] args) {
35.        Student s = new Student();
36.        System.out.println(s.c);
37.        System.out.println(s.bo);
38.        System.out.println(s.ch);
39.        System.out.println(s.d);
40.        System.out.println(s.e);
41.        System.out.println(s.f);
42.        System.out.println(s.name);
43.        System.out.println(s.num2);
44.        System.out.println(s.s);
45.
46.        s.d = 10.4;
47.    }
48.} 
 
 
