public class ClassName {
// instance variables here
boolean yesOrNo;
public ClassName(int val) {/*...*/} // constructor
// constructor overloading is legal
public static void main(String[] args) {}
}
Instantiating
public class Car {
int value1;
boolean value2;
public Car(int val1, boolean val2)
{
value1 = val1;
value2 = val2;
}
public static void main(String[] args)
{
Car ferrari = new Car(1, true);
// create a car instance, note ferrari is a pointer
}
}
Method
// note this is with-in the classes scope
// make method
public int name(int val) {
// code
return val;
}
obj.name(2); // call method
// PRINTING OBJS
// toString will print the returned value instead of the default memory location
public String toString() {return "this is what happens when u print the obj";}
public static void methodName() {}
// static means that an object doenst need to exist to run the method
// it is called by the classname.methodname()
Attribute access
objectName.varName;
Public & Private
// note that protected may be better than private
public class
public attribute
public method()
public constructor()
This
this.varName;
// refures to the instance variable
// why would i use it
public Dog(String name){
this.name = name;
}
methodName(this)
// passing the obj
Static variables
public static int val;
// shaired across the entire class
// note static vars do not work with the 'this' keyword
// since they are associated with the class not the object
// instead you use the class name