Static Examples
The key terms in Static Class introduction:
- “class” is a blueprint for code, it is the code definition and must be called to run
- “method” or “static method” in this case, is the code to be run/executed, similar to a procedure
- “method definition” or “signature” are the keywords “public static void” in front of the name “main” and the parameters “String[] args” after the name.
- “method call” is the means in which we run the defined code
static
is an important keyword, static classes cannot interact with outside classes, and are entirely self-contained and every class you access must be static. This ensures protections from weird values and is used in self-contained objects.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
System.out.println("Hello World!"); // Static example
Hello World!
// Scanner example
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username");
String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName); // Output user input
Enter username
Username is: nVarap
Dynamic Examples
This example starts to use Java in its natural manner, using an object within the main method. This example is a very basic illustration of Object Oriented Programming (OOP). The main method is now used as a driver/tester, by making an instance of the class. Thus, it creates an Object using the HelloObject() constructor. Also, this Class contains a getter method called getHello() which returns the value with the “String hello”.
The key terms in HelloStatic introduction:
- “Object Oriented Programming” focuses software design around data, or objects.
- “object” contains both methods and data
- “instance of a class” is the process of making an object, unique or instances of variables are created within the object
- “constructor” special method in class, code that is used to initialize the data within the object
- “getter” is a method that is used to extract or reference data from within the object.
These create a specific object that can be instantiated, have values manipulated and gotten, and otherwise manipulated in order to create new and different objects. This can be used to abstract numerous different real world things.
// Define Class with Constructor returning Object
public class HelloObject {
private String hello; // instance attribute or variable
public HelloObject(String greeting) { // constructor
hello = greeting;
}
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("Hello World!"); // Instance of Class (ho) is an Object via "new HelloObject()"
HelloObject hi = new HelloObject("Hello, Emaad!"); // another instance with a different greeting
System.out.println(ho.getHello()); // Object allows reference to public methods and data
System.out.println(hi.getHello());
}
}
// IJava activation
HelloObject.main(null);
Hello World!
Hello, Emaad!
Dynamic Example with two constructors
This last example adds to the basics of the Java anatomy. The Class now contains two constructors and a setter to go with the getter. Also, observe the driver/tester now contains two objects that are initialized differently, 0 and 1 argument constructor. Look at the usage of the “this” prefix. The “this” keyword helps in clarification between instance and local variable.
The key terms in HelloDynamic introduction:
- “0 argument constructor” constructor method with no parameter ()
- “1 argument constructor” construct method with a parameter (String hello)
- “this keyword” allows you to clear reference something that is part of the object, data or method
- “local variable” is a variable that is passed to the method in this example, see the 1 argument constructor as it has a local variable “String hello”
- “dynamic” versus “static” is something that has option to change, static never changes. A class (blueprint) and objects (instance of blueprint) are generally intended to be dynamic. Constructors and Setters are used to dynamically change the content of an object.
- “Java OOP, Java Classes/Objects, Java Class Attributes, Java Class Methods, Java Constructors” are explained if more complete detail in W3 Schools: https://www.w3schools.com/java/java_oop.asp
This allows for general input which creates a regular static input, but also a modular constructor that ends up assisting in creating a more diverse range of input.
// 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);
Hello, World!
Hello, Nighthawk Coding Society!
Hacks
- Explain the anatomy of a class
- Person Class (For general use in an account)
- Create a static class (no objects)
Person Dynamic Class
This takes the class and then allows the user to input, but also creates a base class if nothing is inputted (can be used for testing)
public class Person {
String name;
String username;
int age;
String abode;
String password;
public Person(){
name = "Joe Smith";
username = "placeholder";
age = 21;
abode = "San Jose";
password = "password12345";
}
public Person(String n, String u, int a, String ab, String p){
name = n;
username = u;
age = a;
abode = ab;
password = p;
}
public String getName(){
return this.name;
}
public static void main(String [] args){
Person emaad = new Person("Emaad Mir", "emu", 16, "San Diego", "Tesla Model X");
Person me = new Person("Varalu N", "vvn", 16, "San Jose", "ANEWPASSWORD");
Person someone = new Person();
System.out.println(emaad.getName());
System.out.println(me.getName());
System.out.println(someone.getName());
}
}
Person.main(null);
Emaad Mir
Varalu N
Joe Smith
Purposeful Static Class
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public int add(int a, int b){return a+b;}
public int sub(int a, int b){return b-a;}
public int mult(int a, int b){return a*b;}
public int div(int a, int b){return b/a;}
Scanner calc = new Scanner(System.in);
System.out.println("Add, Subtract, Multiply, Divide? (a, s, m, d)");
String choice = calc.nextLine();
System.out.println(choice);
System.out.println("First Number: ");
int a = calc.nextInt();
System.out.println(a);
System.out.println("Second Number: ");
int b = calc.nextInt();
System.out.println(b);
int answer = 0;
if (choice.equals("a")){
answer = add(a, b);
}
else if (choice.equals("s")){
answer = sub(a, b);
}
else if (choice.equals("m")){
answer = mult(a, b);
}
else if (choice.equals("d")){
answer = div(a, b);
}
System.out.println("Your answer is " + answer);
Add, Subtract, Multiply, Divide? (a, s, m, d)
d
First Number:
2
Second Number:
1
Your answer is 0