JAVA中關於類的幾個性質
Client Programs and Main Methods.A Java program without a main method cannot be run directly using thejava
command. However, its methods can still be invoked using themain
method of another class, and theclass that uses another class is sometimes called a "client" of that class
Class Declaration.Java classes can contain methods and/or variables. We say that such methods and variables are “members” of the class. Members can beinstance
static
keyword. Instance members are any members without thestatic
keyword.
Class Instantiation.Instantiating a class is almost always done using thenew
keyword, e.g.Dog d = new Dog()
. An instance of a class in Java is also called an “Object”.
Dot Notation.We access members of a class using dot notation, e.g.d.bark()
. Class members can be accessed from within the same class or from other classes.
Constructors.Constructors tell Java what to do when a program tries to create an instance of a class, e.g. what it should do when it executesDog d = new Dog()
The constructor with signaturepublic Dog(int w)
will be invoked anytime that we try to create aDog
using thenew
keyword and a single integer parameter. For those of you coming from Python, the constructor is very similar to the__init__
method.
Array Instantiation.Arrays are also instantiated using thenew
keyword. For exampleint[] arr = new int[10]
If we have an array of Objects, e.g.Dog[] dogarray
, then each element of the array must also be instantiated separately.
Static vs. Instance methods.The distinction between static and instance methods is incredibly important. Instance methods are actions that can only be taken by an instance of the class (i.e. a specific object), whereas static methods are taken by the class itself. An instance method is invoked using a reference to a specific instance, e.g.d.bark()
, whereas static methods should be invoked using the class name, e.g.Math.sqrt()
. Know when to use each.
Static variables.Variables can also be static. Static variables should be accessed using the class name, e.g.Dog.binomen
as opposed tod.binomen
. Technically Java allows you to access using a specific instance, but we strongly encourage you not to do this to avoid confusion.
void methods.A method which does not return anything should be given a void return type.
Thethis
keyword.Inside a method, we can use thethis
keyword to refer to the current instance. This is equivalent toself
in Python.
public static void main(String[] args).We now know what each of these things means:
- public: So far, all of our methods start with this keyword.
- static: It is a static method, not associated with any particular instance.
- void: It has no return type.
- main: This is the name of the method.
- String[] args: This is a parameter that is passed to the main method.
Command Line Arguments.Arguments can be provided by the operating system to your program as “command line arguments,” and can be accessed using theargs
parameter inmain
. For example if we call our program from the command line like thisjava ArgsDemo these are command line arguments
, then themain
method ofArgsDemo
will have an array containing the Strings “these”, “are”, “command”, “line”, and “arguments”.
Using Libraries.There’s no need in the modern world to build everything yourself from scratch. In our course, you are allowed to and highly encouraged to use Java’s built-in libraries, as well as libraries that we provide, e.g. the Princeton standard library. You should not use libraries other than those provided or built into Java because it may render some of the assignments moot, and also our autograder won’t have access to these libraries and your code won’t work.