Download presentation
Presentation is loading. Please wait.
Published byFarida Hartono Modified over 6 years ago
1
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables) Constructor (method that creates the object) Methods ( get and set the instance variables and other actions as necessary)
2
Instance Variables Properties of the object. What it knows about itself. Created in a field so also called field data private String color; private int legs; private String name; I know my color, how many legs I have and my name.
3
CONSTRUCTOR public className() { Assign instance variables here }
public Bug() { String color = “red”; int legs = 6; String name = “Beetle”; }
4
Parameter ( ) Supplies extra information to a method that it needs to execute. Required for constructors & all methods. public Bug() { public static void main(String[]args) { public void setName(“String name”);
5
Classes create objects from the constructor with the word new
public Bug() { String color = “red”; int legs = 6; String name = “Beetle”; } Bug b = new Bug(); public Bug(String c, int l, String n) { color = c; legs = l; name = n; } Bug b = new Bug(“Red”, 6, “Beetle”); (String, int, String)
6
Methods Accessor Methods: Access information from our instance variables. Instance variable: name name is a String public String getname() { return name; }
7
Mutator Methods Change information in our instance variables. Called setters. Instance Variable name public void setName(String n); name = n; } Doesn’t return anything so it is void.
8
Objects call their method with dot operator.
Bug b = new Bug(); // b is the object public void setName(String n); // is the method b.setName(“Harry”); System.out.println(b.getname()); prints Harry
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.