Download presentation
Presentation is loading. Please wait.
Published byPhilomena Atkins Modified over 8 years ago
2
CSC 212 – Data Structures Lecture 6: Static and non-static members
3
Using Methods Some methods need an object What would Integer.intValue() return if you did not have an Integer object? But some methods do not make sense to use with an object How to instantiate object to call main(String[])? Should you need Integer to call Integer.parseInt()?
4
Static v. Non-static Methods Methods declared static do not need an instance on which to run Behavior defined by object’s class, not any specific instance Integer.parseInt() only depends on definition of “Integer” static methods use parameters, can return data or be void, and should have visibility modifier
5
Making a Method Static Method must include static in declaration public static void main(String args[]) public static int parseInt(String s) public static float max(float a, float b) Behaves (almost) like any other method Parameters & locals work identically Uses normal operators Can call other methods
6
Static vs. Non-static Method Non-static methods have this parameter Aliased to object on which method was called Directly access fields & call other methods Static methods have no such parameter Cannot directly access non-static fields or call non-static methods Can directly use static fields and methods Can access object’s fields and call its classes
7
BadDuck public class BadDuck { private int quackVolume; public static void printVolume(String[] args) { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ” + duck.getVolume()); } public int getVolume() { return quackVolume; } } Compiler error. Without an object, whose volume are we getting? This is fine. There is a duck for which we get the volume.
8
BadDuck public class BadDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak()); } } This compiles. Static methods do not include implicit “this” parameter, so we can call them like this. This is also fine. Since the method is static, however, we will not be able to use the instance.
9
Static Fields Some data do not belong to single objects, but class as a whole Float.MAX_VALUE Citizen.nationalPopulation Instructors.bestProfessor Static fields shared amongst all objects of type All objects see the same value Changing for one object changes for all objects
10
Static Field Example public class Player { private static int numPlayers; public static String topPlayer; private String name; public Player(String newName) { numPlayers += 1; name = newName; } public static int getNumPlayers() { return numPlayers; } public static String setTopPlayer(String name) { topPlayer = name; } } This is still bad. Fields should always * be made private.
11
Tracing With Statics public void startGame() { Player player1, player2, player3; player1 = new Player(“Homer Simpson”); player2 = new Player(“Joey JoJo Jr. Shabadoo”); player3 = player2; Player.topPlayer = “Homer Simpson”; player2.setTopPlayer(“Joey JoJo Jr. Shabadoo”); } Player numPlayers 0 topPlayer “” statics name “Homer Simpson” player1 numPlayers 1 statics name “Joey JoJo Jr. Shabadoo” player2player3 numPlayers 2 topPlayer “Homer Simpson” numPlayers 2 topPlayer “Joey JoJo Jr. Shabadoo”
12
Your Turn Get back into groups and try daily activity
13
Before Next Lecture… Finish lab #1 Start week #3 assignment Get ready for programming assignment #1 It should be easy & you’ll have 2 weeks Wednesday’s lecture discusses I/O Keep thinking & ask any questions you have May want to review any old notes & handouts
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.