Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Java Classes &

Objects
Java Classes/Objects
• Java is an object-oriented programming
language.
• Everything in Java is associated with classes
and objects, along with its attributes and
methods.
• A Class is like an object constructor, or a
"blueprint" for creating objects.
Create a Class
• To create a class, use the keyword class:

MyClass.java
Create a class called "MyClass" with a variable x:
public class MyClass {
  int x = 5;
}
Create an Object
It is a basic unit of Object Oriented Programming and
represents the real life entities.  A typical Java program
creates many objects, which as you know, interact by
invoking methods. An object consists of :
– State : It is represented by attributes of an object. It
also reflects the properties of an object.
– Behavior : It is represented by methods of an object.
It also reflects the response of an object with other
objects.
– Identity : It gives a unique name to an object and
enables one object to interact with other objects.
Create an Object
Create an Object
• In Java, an object is created from a class.
We have already created the class
named MyClass, so now we can use this to
create objects.
• To create an object of MyClass, specify the
class name, followed by the object name,
and use the keyword new:
Create an Object
• Create an object called "myObj" and print the value of
x:
public class MyClass {
  int x = 5;

  public static void main(String[] args) {
    MyClass myObj = new MyClass();
    System.out.println(myObj.x);
  }
}
Create Multiple Objects
• Create two objects of MyClass:

public class MyClass {
  int x = 5;

  public static void main(String[] args) {
    MyClass myObj1 = new MyClass();
    MyClass myObj2 = new MyClass();
    System.out.println(myObj1.x);
    System.out.println(myObj2.x);
  }
}
Using Multiple Classes
• We can also create an object of a class and access
it in another class.
• Remember that the name of the java file should
match the class name. In this example, we have
created two files in the same directory/folder:
– MyClass.java
– OtherClass.java
Java Class Attributes
• Create a class called "MyClass" with two
attributes: x and y:

public class MyClass {
  int x = 5;
  int y = 3;
}
Accessing Attributes
public class MyClass {
  int x = 5;

  public static void main(String[]
args) {
    MyClass myObj = new MyClass();
    System.out.println(myObj.x);
  }
}
Modify Attributes
• There are 2 ways to modify values of attributes:
– Modify Attribute Values
– Override Existing Values

• Declare the attribute as final


Multiple Objects
• If you create multiple objects of one class, you can
change the attribute values in one object, without
affecting the attribute values in the other:

• Change the value of x to 25 in myObj2, and


leave x in myObj1 unchanged:
Multiple Objects
public class MyClass {
  int x = 5;

  public static void main(String[]
args) {
    MyClass myObj1 = new MyClass();
    MyClass myObj2 = new MyClass();
    myObj2.x = 25;
    System.out.println(myObj1.x);
    System.out.println(myObj2.x);
  }}
Multiple Attributes
public class Dog{
  String name = “Max”
String breed = “The Beagle”
int age = 3;

  public static void main(String[] args) {
    Dog myDog = new Dog();
    System.out.println(“Name and Breed: “ +
myDog.name + “ “ + myDog.breed);
    System.out.println(“Age: “ + myDog.age);
  }}
Introduction to
Java Methods
Methods
• A method is a block of code which only runs
when it is called.
• You can pass data, known as parameters, into
a method.
• Methods are used to perform certain actions,
and they are also known as functions.
• Why use methods? To reuse code: define the
code once, and use it many times.
Methods
• A method must be declared within a class.
• It is defined with the name of the method,
followed by parentheses ().
• Java provides some pre-defined methods,
such as System.out.println()
Create a method inside MyClass:

public class MyClass {
  static void myMethod() {
    // code to be executed
  }
}
public class MyClass {
  static void myMethod() {
    // code to be executed
  }
}

• myMethod() is the name of the method


public class MyClass {
  static void myMethod() {
    // code to be executed
  }
}
• static means that the method belongs to
the MyClass class and not an object of the
MyClass class.
public class MyClass {
  static void myMethod() {
    // code to be executed
  }
}

• void means that this method does not have


a return value.
Call a Method
• To call a method in Java, write the method's
name followed by two parentheses () and a
semicolon ;
Call a Method
public class MyClass {
  static void myMethod() {
    System.out.println("I just got
executed!");
  }

  public static void main(String[] args) {
    myMethod();
  }
}
Call a Method
• A method can be also called multiple times:

public class MyClass {
  static void myMethod() {
    System.out.println("I just got executed!");
  }

  public static void main(String[] args) {
    myMethod();
    myMethod();
    myMethod();
  }
}
Method Parameters
• Information can be passed to functions as
parameter. Parameters act as variables
inside the method.
• Parameters are specified after the method
name, inside the parentheses. You can add
as many parameters as you want, just
separate them with a comma.
Method Parameters
public class MyClass {
  static void myMethod(String fname) {
    System.out.println(fname + “Jenner");
  }

  public static void main(String[] args) {
    myMethod(“Kylie");
    myMethod(“Kourtney");
    myMethod(“Kendall");
  }
}
Return Values
• The void keyword, used in the examples
above, indicates that the method should
not return a value.
• If you want the method to return a value,
you can use a primitive data type (such
as int, char, etc.) instead of void.
Return Values
public class MyClass {
  static int myMethod(int x) {
    return 5 + x;
  }

  public static void main(String[]
args) {
    System.out.println(myMethod(3));
  }
}
Return Values
public class MyClass {
  static int myMethod(int x, int y) {
    return x + y;
  }

  public static void main(String[]
args) {
    System.out.println(myMethod(5, 3));
  }
}
Return Values
public class MyClass {
  static int myMethod(int x, int y) {
    return x + y;
  }

  public static void main(String[]
args) {
    int z = myMethod(5, 3);
    System.out.println(z);
  }
}
A Method with if...else
public class MyClass {
  static void checkNum(int num) {
    if (num % 2==0) {
      System.out.println(“The number is
even!"); } 
else {
      System.out.println(“The number is
odd!"); }

  public static void main(String[] args) { 
    checkNum(20);  } }

You might also like