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 {
int age;
String name;
// behavior of an object
age = 20;
name = "Robin";
// main method
p.set_value();
}
Java instance
Java instances are objects that are based on classes. For
example, Bob may be an instance of the class Person . public class Person {
Every instance has access to its own set of variables int age;
which are known as instance fields, which are variables String name;
method.
public Person(int age, String name) {
this.age = age;
this.name = name;
This is known as dot notation and the structure looks like int age;
this-
instanceOrClassName.fieldOrMethodName
public static void main(String [] args)
{
p.age = 20;
// Output: Age is 20
}
Constructor Method in Java
Java classes contain a constructor method which is used
to create instances of the class. public class Maths {
System.out.println("I am main");
instance fields.
public Person(int a) {
age = a;
}
Reference Data Types
A variable with a reference data type has a value that
references the memory address of an instance. During public class Cat {
System.out.println(garfield); //
Prints: Cat@76ed5528
Constructor Signatures
A class can contain multiple constructors as long as they
have different parameter values. A signature helps the // The signature is `Cat(String furLength,
compiler differentiate between the different boolean hasClaws)`.
furType = furLength;
containsClaws = hasClaws;
}
null Values
null is a special value that denotes that an object has a
void reference. public class Bear {
String species;
species = speciesOfBear;
System.out.println(baloo); // Prints:
Bear@4517d9a3
baloo = null;
System.out.println(baloo); // Prints:
null
The statements written inside the {} are executed when public static void sum(int a, int b) {
a method is called. // Start of sum
int result = a + b;
System.out.println("Sum is " +
result);
} // End of sum
sum(10, 20);
// Output: Sum is 30
}
Method parameters in Java
In java, parameters are declared in a method definition.
The parameters act as variables inside the method and public class Maths {
hold the value that was passed in. They can be used public int sum(int a, int b) {
respectively.
System.out.println("sum is " +
result);
// prints - sum is 30
int i, j;
System.out.println("These two
variables are available in main method
only");
The value is returned using the return keyword. // return type is int
int k;
k = a + b;
return k;
int result;
System.out.println("Sum is " +
result);
// Output: Sum is 30
Declaring a Method
Method declarations should define the following method
information: scope (private or public), return type, // Here is a public method named sum whose
method name, and any parameters it receives. return type is int and has two int
parameters a and b
return(a + b);