Java Anatomy
Java Anatomy
How does it work?
First, you need to create a class. The name of the class is sometimes called the definition or the header. The keyword at the start determines its access level. Inside of the class, you can create methods, which are like functions. A static method ensures that there is only one of them. The public keyword determines the access level of the method. Void just means that the method doesn't return anything.
We can then call this method without first instantiating the object, because the method was static.
// Define Static Method within a Class
public class HelloStatic {
// Java standard runtime entry point
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
// A method call allows us to execute code that is wrapped in Class
HelloStatic.main(null); // Class prefix allows reference of Static Method
Constructors
A class can have a constructor. This is basically created with the syntax of excluding the "class" keyword. Inside the constructor, you can define and assign variables. Make sure that the variable is defined with an access level and a datatype before it is included in the constructor.
A getter is basically so that you can access variables with the private keyword from outside of the class. It is simply a public method that returns the value of a particular private variable. Note that this is not static, so an object of the class must be created to call this method.
Constructors do not return anything because a constuctor is called by the memory allocation and object initialization code in the runtime. The runtime returns the instance of the object.
// Define Class with Constructor returning Object
public class HelloObject {
private String hello; // instance attribute or variable
public HelloObject() { // constructor
hello = "Hello, World!";
}
public String getHello() { // getter, returns value from inside the object
return this.hello; // return String from object
}
public static void main(String[] args) {
HelloObject ho = new HelloObject(); // Instance of Class (ho) is an Object via "new HelloObject()"
System.out.println(ho.getHello()); // Object allows reference to public methods and data
}
}
// IJava activation
HelloObject.main(null);
Multiple Objects and Constructors
The below class has two constructors. When a class object is created with no parameters, the first constructor is called. When a class object is created with a parameter, the second constructor is called. This allows for a user to access different functionalities of the program based not only on what the values are of the parameters they type but also on what parameters they actually choose to type.
This method also has a getter and a setter for the private hello string.
// Define Class
public class HelloDynamic { // name the first letter of class as capitalized, note camel case
// instance variable have access modifier (private is most common), data type, and name
private String hello;
// constructor signature 1, public and zero arguments, constructors do not have return type
public HelloDynamic() { // 0 argument constructor
this.setHello("Hello, World!"); // using setter with static string
}
// constructor signature, public and one argument
public HelloDynamic(String hello) { // 1 argument constructor
this.setHello(hello); // using setter with local variable passed into constructor
}
// setter/mutator, setter have void return type and a parameter
public void setHello(String hello) { // setter
this.hello = hello; // instance variable on the left, local variable on the right
}
// getter/accessor, getter used to return private instance variable (encapsulated), return type is String
public String getHello() { // getter
return this.hello;
}
// public static void main(String[] args) is signature for main/drivers/tester method
// a driver/tester method is singular or called a class method, it is never part of an object
public static void main(String[] args) {
HelloDynamic hd1 = new HelloDynamic(); // no argument constructor
HelloDynamic hd2 = new HelloDynamic("Hello, Nighthawk Coding Society!"); // one argument constructor
System.out.println(hd1.getHello()); // accessing getter
System.out.println(hd2.getHello());
}
}
// IJava activation
HelloDynamic.main(null);