【Java基础】Java关键字 ——static

 static:静态的,static可以用来修饰:属性、方法、代码块、内部类

 1.使用static修饰属性:静态变量(或类变量)

              属性,按是否使用static修饰,又分为:静态属性  vs 非静态属性(实例变量)

    实例变量:我们创建了类的多个对象,每个对象都独立的拥有一套类中的非静态属性。当修改     其中一个对象中的非静态属性时,不会导致其他对象中同样的属性值的修改。

    静态变量:我们创建了类的多个对象,多个对象共享同一个静态变量。当通过某一个对象修改静态变量时,会导致其他对象调用此静态变量时,是已经修改过了的。

              static修饰属性的其他说明:

 ① 静态变量随着类的加载而加载。可以通过"类.静态变量"的方式进行调用

 ② 静态变量的加载要早于对象的创建。

 ③ 由于类只会加载一次,则静态变量在内存中也只会存在一份:存在方法区的静态域中

 2..使用static修饰方法:静态方法

 ① 随着类的加载而加载,可以通过"类.静态方法"的方式进行调用

 ② 静态方法 非静态方法

 ③ 静态方法中,只能调用静态的方法或属性,非静态方法中,既可以调用非静态的方法或属性,也可以调用静态的方法或属性

.              static注意点:

1 在静态的方法内,不能使用this关键字、super关键字

2 关于静态属性和静态方法的使用,可以从他们的生命周期的角度去理解。

               实际开发中,如何确定一个属性是否要声明为static的?

  > 属性是可以被多个对象所共享的,不会随着对象的不同而不同的。

 > 类中的常量也常常声明为static

              开发中,如何确定一个方法是否要声明为static的?

  > 操作静态属性的方法,通常设置为static的

 > 工具类中的方法,习惯上声明为static的。 比如:Math、Arrays、Collections

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个使用双向链表作为数据结构的通讯录管理系统的 Java 代码,包括输入信息、显示信息、查找、删除、存盘和装入等功能: ```java import java.io.*; import java.util.Scanner; class Contact { String name; String street; String city; String zip; String state; Contact prev; Contact next; public Contact(String name, String street, String city, String zip, String state) { this.name = name; this.street = street; this.city = city; this.zip = zip; this.state = state; prev = null; next = null; } public void display() { System.out.println("Name: " + name); System.out.println("Street: " + street); System.out.println("City: " + city); System.out.println("Zip: " + zip); System.out.println("State: " + state); } } class ContactList { Contact head; Contact tail; public ContactList() { head = null; tail = null; } public void enter() { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); String name = sc.nextLine(); System.out.print("Enter street: "); String street = sc.nextLine(); System.out.print("Enter city: "); String city = sc.nextLine(); System.out.print("Enter zip: "); String zip = sc.nextLine(); System.out.print("Enter state: "); String state = sc.nextLine(); Contact newContact = new Contact(name, street, city, zip, state); if (head == null) { head = newContact; tail = newContact; } else { tail.next = newContact; newContact.prev = tail; tail = newContact; } } public void display() { Contact current = head; while (current != null) { current.display(); current = current.next; } } public void search() { Scanner sc = new Scanner(System.in); System.out.print("Enter name to search: "); String name = sc.nextLine(); Contact current = head; boolean found = false; while (current != null) { if (current.name.equalsIgnoreCase(name)) { current.display(); found = true; } current = current.next; } if (!found) { System.out.println("No contact found with name " + name); } } public void delete() { Scanner sc = new Scanner(System.in); System.out.print("Enter name to delete: "); String name = sc.nextLine(); Contact current = head; boolean found = false; while (current != null) { if (current.name.equalsIgnoreCase(name)) { found = true; if (current == head) { head = current.next; if (head != null) { head.prev = null; } } else if (current == tail) { tail = current.prev; if (tail != null) { tail.next = null; } } else { current.prev.next = current.next; current.next.prev = current.prev; } System.out.println("Contact deleted."); break; } current = current.next; } if (!found) { System.out.println("No contact found with name " + name); } } public void save() { try { FileOutputStream fos = new FileOutputStream("contacts.dat"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(head); oos.close(); fos.close(); System.out.println("Contacts saved to file."); } catch (IOException e) { e.printStackTrace(); } } public void load() { try { FileInputStream fis = new FileInputStream("contacts.dat"); ObjectInputStream ois = new ObjectInputStream(fis); head = (Contact) ois.readObject(); ois.close(); fis.close(); System.out.println("Contacts loaded from file."); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ContactList list = new ContactList(); while (true) { System.out.println("1. Enter contact"); System.out.println("2. Display contacts"); System.out.println("3. Search contact"); System.out.println("4. Delete contact"); System.out.println("5. Save contacts to file"); System.out.println("6. Load contacts from file"); System.out.println("0. Exit"); System.out.print("Enter your choice: "); int choice = sc.nextInt(); sc.nextLine(); // consume newline switch (choice) { case 1: list.enter(); break; case 2: list.display(); break; case 3: list.search(); break; case 4: list.delete(); break; case 5: list.save(); break; case 6: list.load(); break; case 0: System.exit(0); break; default: System.out.println("Invalid choice."); } } } } ``` 在这个实现中,`Contact` 类表示一个联系人,包含姓名、街道、城市、邮编和国家等信息,以及前后指针。`ContactList` 类表示一个联系人列表,维护头尾指针,并提供输入、显示、查找、删除、存盘和装入等方法。主函数提供了一个菜单,用户可以选择不同的功能。通讯录的信息可以保存到文件中,也可以从文件中装入。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小尘要自信

不要打赏,我不配。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值