Summary of practical cases of Java static method usage

This article describes the usage of Java static method with examples. Share with you for your reference, as follows:

A finishing touch

Static can be used to declare a static attribute variable. In fact, it can also be used to declare a method. When it is used to declare a method, it is also called "class method". Methods defined using static can be called directly by the class name.

Static's main method can receive an array parameter of string type, which holds the parameters passed to the running class when executing Java commands. The format is as follows:

Java class name parameter 1 Parameter 2 parameter 3

II. Actual combat - declaration of static methods

1 code

class Person
{
  String name;             //定义name属性
  private static String nation = "中国"; //定义静态属性nation
  int age;              //定义age属性
  public Person(String name,int age) //声明一个有参的构造方法
  {
    this.name = name;
    this.age = age;
  }
  public String talk()        //声明了一个talk()方法
  {
    return "我是:" + this.name + ",今年:" + this.age + "岁,来自:" + nation;
  }
  public static void setNation(String nat)//声明一个静态方法,给静态变量赋值
  {
    nation = nat;
  }
}
public class StaticMethod
{
  public static void main(String[] args)
  {
    Person p1 = new Person("张三",25);
    Person p2 = new Person("李四",30);
    Person p3 = new Person("王五",35);
    System.out.println("修改之前信息:" + p1.talk());
    System.out.println("修改之前信息:" + p2.talk());
    System.out.println("修改之前信息:" + p3.talk());
    System.out.println("  **************修改之后信息**************");
    // 修改后的信息
    Person.setNation("美国");
    System.out.println("修改之后信息:" + p1.talk());
    System.out.println("修改之后信息:" + p2.talk());
    System.out.println("修改之后信息:" + p3.talk());
  }
}

2 operation

3 description

Static type method: only static type properties can be called, and non static type properties cannot be called.

Non static methods: you can call both static and non static properties.

Three practical operations -- passing parameters to the main method

1 code

/*
* public:表示公共方法
* static:表示此方法为一静态方法,可以由类名直接调用
* void:表示此方法无返回值 main:系统定义的方法名称
* String args[]:接收运行时参数
*/
public class TestMain
{
  public static void main(String[] args)
  {
    //取得输入参数的长度
    int len = args.length;
    System.out.println("输入的参数个数:" + args.length);
    if(len < 2)
    {
      System.out.println("输入参数个数有误!" );
      //退出程序
      System.exit(-1);
    }
    for(int i = 0; i < args.length; i++)
    {
      System.out.println( args[i] );
    }
  }
}

2 operation

IV. actual combat -- using scanner to pass parameters to the main method

1 code

import java.util.Scanner;
public class ScannerTest
{
   public static void main(String[] Test)
   {
      Scanner sc = new Scanner(system.in);
      while (sc.hasNext())
      {
        System.out.println("键盘输入的内容是:" + sc.next());
      }
      sc.close();
   }
}

2 operation

For more Java related content, interested readers can view the special topics of this site: introduction and advanced tutorial of java object-oriented programming, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

I hope this article will be helpful to you in Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>