Learn Java - Object-Oriented Java Cheatsheet - Codecademy
Learn Java - Object-Oriented Java Cheatsheet - Codecademy
Object-Oriented Java
Java objects’ state and behavior
In Java, instances of a class are known as objects. Every
object has state and behavior in the form of instance public class Person {
fields and methods respectively. // state of an object
int age;
String name;
// behavior of an object
public void set_value() {
age = 20;
name = "Robin";
}
public void get_value() {
System.out.println("Age is " + age);
System.out.println("Name is "
+ name);
}
// main method
public static void main(String [] args)
{
// creates a new Person object
Person p = new Person();
Constructor Signatures
A class can contain multiple constructors as long as
they have different parameter values. A signature helps // The signature is `Cat(String
the compiler differentiate between the different furLength, boolean hasClaws)`.
constructors. public class Cat {
A signature is made up of the constructor’s name and a String furType;
list of its parameters.
boolean containsClaws;
Declaring a Method
Method declarations should define the following
method information: scope (private or public), return // Here is a public method named sum
type, method name, and any parameters it receives. whose return type is int and has two int
parameters a and b
public int sum(int a, int b) {
return(a + b);
}