static关键字的特点:它可以修饰成员变量个成员方法
1、随着类的加载而加载
2、是优先于对象而存在的
3、被类的所有对象共享
举例:所有中国人的国籍信息都是中国都是一样。 什么时候使用静态static关键字呢? 如果某个成员变量是被所有的对象共享的,值是一样的,那么它就应该被定义为static静态的。 举例现实生活的案例: 哈罗共享单车(可以用静态的修饰) 自己的水杯(不可以用静态的修饰)
4、可以通过类名直接调用
一般情况下我们只要看到有一个类中有静态修饰的成员变量或者成员方法
我们一律推荐使用:类名.静态成员 这种方法去使用;
静态修饰的内容我们一般称为:类成员,类相关的。
class Student2{
//非静态的成员变量
int num = 20;
//静态的成员变量
static int num2 = 30;
}
public class StaticDemo1 {
public static void main(String[] args) {
Student2 s1 = new Student2();
System.out.println(s1.num);
// System.out.println(s1.num2);
System.out.println(Student2.num2);
}
}
static关键字使用注意事项:
在静态方法中是没有this关键字的;
this代表的是当前调用该方法的对象,而被static修饰的成员是优先于对象而存在的,被static修饰的成员是随着类的加载而加载,这时候还没有对象产生,就说明这时候还没有this关键字。
静态只能访问静态的:
class Student3 {
private String name;
private int age;
private static int a;
public Student3() {
}
public Student3(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// public static int getAgeAgain(){
// return this.age;
// }
public static void fun1(){
//尝试访问非静态的成员变量name
//无法从静态上下文中引用非静态 变量 name
//静态的成员方法不能访问非静态的成员变量
// System.out.println(name);
//尝试访问静态的成员变量a
//静态的成员方法可以访问静态成员变量
System.out.println(a);
//尝试访问非静态的成员方法show()
//无法从静态上下文中引用非静态 方法 show()
//静态的成员方法不能访问非静态的成员方法
// show();
//尝试方法静态的成员方法function()
//静态的成员方法可以访问静态的成员方法
function();
}
public void fun2(){
//尝试访问非静态的成员变量name
//非静态的成员变量可以访问非静态的成员方法
System.out.println(name);
//尝试访问静态的成员变量a
//非静态的成员方法可以访问静态成员变量
System.out.println(a);
//尝试访问非静态的成员方法show()
//非静态的成员方法可以方法非静态的成员方法
show();
//尝试方法静态的成员方法function()
//非静态的成员方法可以访问静态的成员方法
function();
}
public static void function(){
}
public void show() {
System.out.println(name + "---" + age);
}
}
public class StaticDemo2 {
public static void main(String[] args) {
}
}
成员方法:
1、有返回值的;(void)
2、没有返回值的;
形式参数:
1、没有参数的方法;
2、带参数的方法;
class Function {
//定义一个没有返回值没有参数的成员方法
public void fun1() {
System.out.println("这是一个没有返回值,没有参数的方法");
}
//定义一个没有返回值,带参数的方法
public void fun2(String name) {
System.out.println("这是带参数无返回值的方法:" + name);
}
//定义有返回值没有参数的方法
public String fun3() {
return "数加科技";
}
//定义一个有返回值有参数的方法
public String fun4(String s) {
return s + "你好";
}
}
public class FunctionDemo {
public static void main(String[] args) {
//创建Function类的对象
Function function = new Function();
//没有返回值的方法,直接调用可以输出结果
function.fun1();
function.fun2("15期");
//有返回值的方法,调用的时候,需要用一个变量接收返回值或者直接使用
String s1 = function.fun3();
System.out.println(s1);
String s2 = function.fun4("明旺");
System.out.println(s2);
}
}
工具类创建注意事项:
1、构造方法私有化,不能让外界创建对象;
2、将工具类中的方法用静态修饰,可以让外界通过类名访问;
如何制作说明书:
1、工具类
2、借助注释生成帮助文档
格式:javadoc -d 目录 -author -version ArrayTool.java
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {21,32,55,11,2,10};
//静态的方法只能方法静态的成员方法
// printArray(arr);
//通过使用工具类去遍历数组
ArrayTool.printArray(arr);
//工具类不允许外界随意改动
//不能外界创建对象
//如何让外界不能使用工具类创建对象呢?
// ArrayTool arrayTool = new ArrayTool();
//将构造方法私有化
// System.out.println();
// int[] arr2 = niXu(arr);
// printArray(arr2);
}
// public static void printArray(int[] array){
// for(int i=0;i<array.length;i++){
// if(i==array.length-1){
// System.out.print(array[i]+"]");
// }else if(i==0){
// System.out.print("["+array[i]+",");
// }else {
// System.out.print(array[i]+",");
// }
// }
// System.out.println();
// }
//
// public static int[] niXu(int[] array){
// for(int start=0,end=array.length-1;start<=end;start++,end--){
// int temp = array[start];
// array[start] = array[end];
// array[end] = temp;
// }
// return array;
// }
}
package com.shujia.wyh.day09;
/**
* 这是针对数组相关操作的工具类
* @author 小虎
* @version V.1.0
*
*/
public class ArrayTool {
/**
* 这是私有的无参构造方法
*/
private ArrayTool(){
}
/**
* 这是遍历数组的方法,遍历后的格式是[元素1,元素2,元素3...]
* @param array 这是调用该方法时需要传入的参数,数据类型是int类型的数组
*/
public static void printArray(int[] array){
for(int i=0;i<array.length;i++){
if(i==array.length-1){
System.out.print(array[i]+"]");
}else if(i==0){
System.out.print("["+array[i]+",");
}else {
System.out.print(array[i]+",");
}
}
System.out.println();
}
/**
* 这是数组逆序的方法
* @param array 这是调用该方法时需要传入的参数,传入的数据类型是int类型的数组
* @return 返回逆序后的数组
*/
public static int[] niXu(int[] array){
for(int start=0,end=array.length-1;start<=end;start++,end--){
int temp = array[start];
array[start] = array[end];
array[end] = temp;
}
return array;
}
}