0% found this document useful (0 votes)
3 views8 pages

02. Object-Oriented Programming

The document provides a comprehensive overview of Java programming concepts, focusing on object-oriented principles such as objects, classes, constructors, and methods. It explains the state and behavior of objects, the use of dot notation, and how to create instances of classes. Additionally, it covers method parameters, returning values, and the scope of variables within methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

02. Object-Oriented Programming

The document provides a comprehensive overview of Java programming concepts, focusing on object-oriented principles such as objects, classes, constructors, and methods. It explains the state and behavior of objects, the use of dot notation, and how to create instances of classes. Additionally, it covers method parameters, returning values, and the scope of variables within methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Firefox about:srcdoc

Cheatsheets / Learn Java

Object-Oriented Java

Java objects’ state and behavior

In Java, instances of a class are known as objects. Every public class Person {
object has state and behavior in the form of instance
// state of an object
�elds and methods respectively.
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();

// changes state through behavior


p.set_value();
}
}

1 of 8 7/3/25, 18:05
Firefox about:srcdoc

Java instance

Java instances are objects that are based on classes. public class Person {
For example, Bob may be an instance of the class
int age;
Person .
Every instance has access to its own set of variables
String name;
which are known as instance �elds, which are variables
declared within the scope of the instance. Values for // Constructor method
instance �elds are assigned within the constructor
method.
public Person(int age, String name) {
this.age = age;
this.name = name;
}

public static void main(String[] args)


{
Person Bob = new Person(31, "Bob");
Person Alice = new Person(27,
"Alice");
}
}

Java dot notation

In Java programming language, we use . to access the public class Person {


variables and methods of an object or a Class.
int age;
This is known as dot notation and the structure looks
like this-
instanceOrClassName.fieldOrMethodNam public static void main(String [] args)
e {
Person p = new Person();

// here we use dot notation to set


age
p.age = 20;

// here we use dot notation to access


age and print
System.out.println("Age is " +
p.age);
// Output: Age is 20
}
}

2 of 8 7/3/25, 18:05
Firefox about:srcdoc

Constructor Method in Java

Java classes contain a constructor method which is public class Maths {


used to create instances of the class.
public Maths() {
The constructor is named after the class. If no
constructor is de�ned, a default empty constructor is System.out.println("I am
used. constructor");
}
public static void main(String [] args)
{
System.out.println("I am main");
Maths obj1 = new Maths();
}
}

Creating a new Class instance in Java

In Java, we use the new keyword followed by a call to public class Person {
the class constructor in order to create a new instance
int age;
of a class.
The constructor can be used to provide initial values to // Constructor:
instance �elds. public Person(int a) {
age = a;
}

public static void main(String [] args)


{
// Here, we create a new instance of
the Person class:
Person p = new Person(20);
System.out.println("Age is " +
p.age); // Prints: Age is 20
}
}

3 of 8 7/3/25, 18:05
Firefox about:srcdoc

Reference Data Types

A variable with a reference data type has a value that public class Cat {
references the memory address of an instance. During
public Cat() {
variable declaration, the class name is used as the
variable’s type. // instructions for creating a Cat
instance
}

public static void main(String[] args)


{
// garfield is declared with
reference data type `Cat`
Cat garfield = new Cat();
System.out.println(garfield); //
Prints: Cat@76ed5528
}
}

Constructor Signatures

A class can contain multiple constructors as long as // The signature is `Cat(String


they have di�erent parameter values. A signature helps
furLength, boolean hasClaws)`.
the compiler di�erentiate between the di�erent
constructors. public class Cat {
A signature is made up of the constructor’s name and a String furType;
list of its parameters.
boolean containsClaws;

public Cat(String furLength, boolean


hasClaws) {
furType = furLength;
containsClaws = hasClaws;
}
public static void main(String[] args)
{
Cat garfield = new Cat("Long-hair",
true);
}
}

4 of 8 7/3/25, 18:05
Firefox about:srcdoc

null Values

null is a special value that denotes that an object has public class Bear {
a void reference.
String species;
public Bear(String speciesOfBear;) {
species = speciesOfBear;
}

public static void main(String[] args)


{
Bear baloo = new Bear("Sloth bear");
System.out.println(baloo); // Prints:
Bear@4517d9a3
// set object to null
baloo = null;
System.out.println(baloo); // Prints:
null
}
}

The body of a Java method

In Java, we use curly brackets {} to enclose the body public class Maths {
of a method.
public static void sum(int a, int b) {
The statements written inside the {} are executed
when a method is called.
// Start of sum
int result = a + b;
System.out.println("Sum is " +
result);
} // End of sum

public static void main(String [] args)


{
// Here, we call the sum method
sum(10, 20);
// Output: Sum is 30
}
}

5 of 8 7/3/25, 18:05
Firefox about:srcdoc

Method parameters in Java

In java, parameters are declared in a method de�nition. public class Maths {


The parameters act as variables inside the method and
public int sum(int a, int b) {
hold the value that was passed in. They can be used
inside a method for printing or calculation purposes. int k = a + b;
In the example, a and b are two parameters which, return k;
when the method is called, hold the value 10 and 20
}
respectively.

public static void main(String [] args)


{
Maths m = new Maths();
int result = m.sum(10, 20);
System.out.println("sum is " +
result);
// prints - sum is 30
}
}

Java Variables Inside a Method

Java variables de�ned inside a method cannot be used //For example, `i` and `j` variables are
outside the scope of that method.
available in the `main` method only:

public class Maths {


public static void main(String [] args)
{
int i, j;
System.out.println("These two
variables are available in main method
only");
}
}

6 of 8 7/3/25, 18:05
Firefox about:srcdoc

Returning info from a Java method

A Java method can return any value that can be saved public class Maths {
in a variable. The value returned must match with the
return type speci�ed in the method signature.
The value is returned using the return keyword. // return type is int
public int sum(int a, int b) {
int k;
k = a + b;

// sum is returned using the return


keyword
return k;
}

public static void main(String [] args)


{
Maths m = new Maths();
int result;
result = m.sum(10, 20);
System.out.println("Sum is " +
result);
// Output: Sum is 30
}
}

Declaring a Method

Method declarations should de�ne the following // Here is a public method named sum
method information: scope (private or public), return
whose return type is int and has two int
type, method name, and any parameters it receives.
parameters a and b
public int sum(int a, int b) {
return(a + b);
}

Print Share

7 of 8 7/3/25, 18:05
Firefox about:srcdoc

8 of 8 7/3/25, 18:05

You might also like