public class HellWorld{
String name = “Java”;//非静态变量
static String hobby = “ programing”;//静态变量
public static void print(){
System.out.println(“欢迎您:” + name + ” !”);//不能直接调用非静态变量
System.out.println(“欢迎您:” + hobby + ” !”);//可以直接调用静态变量
}
}
public class HellWorld{
String name = “Java”;//非静态变量
static String hobby = “program ”;//静态变量
//静态方法中调用非静态变量
public static void print(){
//创建类的对象
HelloWorld hello=new HelloWorld();
//通过对象来实现在静态方法中调用非静态变量
System.out.println(“欢迎您:”+hello.name+”!”);
//静态方法中可以直接调用静态变量
System.out.prinltn(“欢迎喜欢”+program+”的”+hello.name);
}
}
public class HellWorld{
String name = “Java”;//非静态变量
static String hobby = “ programing”;//静态变量
public void print(){//普通方法
System.out.println(“欢迎您:” + name + ” !”);
System.out.prinltn(“欢迎喜欢”+program+”的”+hello.name);
}
}
public class HellWorld{
String name = “Java”;//非静态变量
static String hobby = “program ”;//静态变量
//非静态方法
public void show(){
System.out.println(“我是非静态方法,不能被静态方法直接调用。。。”);
}
//静态方法
public static void show2(){
System.out.println(“我是静态方法,可以直接被静态方法调用”);
}
//静态方法中通过对象来调用非静态方法。可以直接调用静态方法
public static void print(){
//创建类的对象
HelloWorld hello=new HelloWorld();
//通过对象来实现在静态方法中调用非静态方法
hello.show();
//静态方法中可以直接调用静态方法
show2();
}
}
public class StaticDemo {
int num1;//声明变量1
int num2;//声明变量2
static int num3;//声明静态变量3
public StaticDemo(){//构造方法
num1 = 88;
System.out.println("通过构造方法为变量1赋值");
}
{//初始化块
num2 = 99;
System.out.println("通过初始化块为变量2赋值");
}
static{//静态初始化这里不能giel普通变量赋值
num3 = 77;
System.out.println("通过静态初始化块为静态变量3赋值");
}
public static void main(String[] args) {
StaticDemo hello = new StaticDemo();//创建类的对象hello
System.out.println("num1:" + hello.num1);
System.out.println("num2:" + hello.num2);
System.out.println("num3:" + hello.num3);
StaticDemo hello1 = new StaticDemo();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有