Download presentation
Presentation is loading. Please wait.
Published byAbraham Long Modified over 8 years ago
1
Java 5 Class Anatomy
2
User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating classes provides us with more flexibility. See book example, p. 214
3
Visualizing Class Files 1 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Instance variables or the data fields (properties) of the object. These are private and can only be accessed by local methods.
4
Visualizing Class Files 2 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Constructor builds the object and initializes any default values. Objects do not require a constructor.
5
Visualizing Class Files 3 public class account { private int acctNumber; private double balance; public account(int a, double b) { acctNumber = a; balance = b; } public void printBalance() { System.out.println(“Bal: “ + balance); } Methods are what an object can do. This method prints out the balance of our account.
6
Using a Class (driver) public class accountRun { public static void main(String[] args) { account a = new account(325,63000.27); a.printBalance(); } Notice the driver file has a main method and creates a new object using the constructor and rules defined in the account class.
7
Instance Data An object is an instance of a class. Variables defined in classes are governed by the concept of “variable scope” See code example on web site. Java automatically initializes class variables but it is good practice to do it anyway.
8
Encapsulation At the object level we think of how to design the class. At the project level we think of how to design objects and how those objects will interact with each other. Objects should be “self-governing” Only the class that declares a variable should be able to change that variable!
9
Encapsulation Pt. 2 Keywords help us set up this protection. public&private private int total = 5; public void setTotal(int a) { total = a; }
10
Basic Methods Modifier (public or private) Return type (void, int, float, string, etc.) Identifier (its name) Parameters (arguments to method) public void sayHello() { System.out.println(“Hello”); }
11
Constructors Methods with the same name as the class called when new objects are initialized. Return nothing. Not required, Java has a built-in constructor if a class doesn’t define one. Jason myObj = new Jason();
12
Return Values Methods can return values to the calling code. public int even(int a) { if(a % 2 == 0) return 1; else return 0; }
13
Method Parameters Parameters must be passed into a method in the proper order. The parameter along with the method’s name is called its “signature”. Signature does not include return type. Method parameters are local to the method in scope.
14
Method Overloading A class can contain several methods with the same name that differ only by their parameters. This is called method overloading. Methods are distinguished by their signature. An example of an overloaded method we’ve used frequently is println().
15
Method Decomposition Sometimes we want to write a method that is too big and cumbersome and so we break it down into multiple, smaller methods. Always try to break your code down into the smallest sensible blocks to make maintenance easier. Too many small methods may actually complicate things so be careful.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.