• Tidak ada hasil yang ditemukan

Instance vs. Class Variables

N/A
N/A
Protected

Academic year: 2023

Membagikan "Instance vs. Class Variables"

Copied!
13
0
0

Teks penuh

(1)

CompSci 230 Software Construction

Lecture Slides #4: Introduction to OOD Part 2 S1 2016

(2)

Agenda

Topics:

Static (class) variables & methods vs. instance variables & methods

(3)

Instance vs. Class Variables

Class variables are statically allocated, so they

are shared by an entire class of objects.

The runtime system allocates class variables once per class, regardless of the number of instances created of that class.

Static storage is allocated when the class is loaded.

All instances share the same copy of the class variables.

In Java, class variables are declared as static.

Instance variables are dynamically allocated, so they

may have different values in each instance of an object.

When an object is instantiated, the runtime system allocates some

memory to this instance – so that it can “remember” the values it stores in instance variables.

(4)

Instance & Class Methods

Instance methods operate on the object's instance variables.

They also have read & write access to class variables.

They can call all other methods in the class: instance methods or class methods

Class methods are static.

Class methods cannot access instance variables.

Class methods can access class variables.

Class methods are handled by the “class object” – they can be called even if there are no instances of this class.

(Example on the next slide.)

(5)

Example: MixedCounter

public class MixedCounter {

private static int sharedCounterValue = 0;

private int counterValue = 0;

public int count() {

sharedCounterValue++;

return ++counterValue;

}

public int getInstanceCount() { return counterValue;

}

public static int getSharedCount() { return sharedCounterValue;

} } Class variable (static)

Instance variable (not static) Note: When the

initialization value is available, we can initialize

in the declaration rather than in the constructor count() method is an

instance method – increments both the instance (individual object)

and shared counter value This getter returns the count stored in the object

This getter returns the count stored in the class

Question: Can we use this class as a counter without instantiating it?

(6)

Using the MixedCounter class and objects

public class MixedCounterProgram {

public static void main(String[] args) { MixedCounter c1 new MixedCounter();

MixedCounter c2 new MixedCounter();

c1.count();

c2.count();

c2.count();

c1.count();

c1.count();

System.out.println("c1.getInstanceCount() returns " + c1.getInstanceCount()); 

System.out.println("c1.getSharedCount() returns " + c1.getSharedCount()); 

System.out.println("c2.getInstanceCount() returns " + c2.getInstanceCount()); 

System.out.println("c2.getSharedCount() returns " + c2.getSharedCount()); 

System.out.println("MixedCounter.getSharedCount() returns " +  MixedCounter.getSharedCount()); 

} }

(7)

Using the MixedCounter class and objects

public class MixedCounterProgram {

public static void main(String[] args) { MixedCounter c1 new MixedCounter();

MixedCounter c2 new MixedCounter();

c1.count();

c2.count();

c2.count();

c1.count();

c1.count();

System.out.println("c1.getInstanceCount() returns " + c1.getInstanceCount()); 

System.out.println("c1.getSharedCount() returns " + c1.getSharedCount()); 

System.out.println("c2.getInstanceCount() returns " + c2.getInstanceCount()); 

System.out.println("c2.getSharedCount() returns " + c2.getSharedCount()); 

System.out.println("MixedCounter.getSharedCount() returns " +  MixedCounter.getSharedCount()); 

} }

Invoking a static (class) method on the class itself

Invoking a static (class) method on an object works in practice but is considered improper

(will produce a warning)

c1.getInstanceCount() returns 3 c1.getSharedCount() returns 5 c2.getInstanceCount() returns 2 c2.getSharedCount() returns 5

MixedCounter.getSharedCount() returns 5

Console output:

(8)

UML

Unified Modeling Language (UML)

When creating complex OO systems, where do we start?

When building complex systems, it might be worthwhile to plan things out before you start coding!

When building a house, we usually have a set of plans.

UML is a language which allows us to graphically model an OO system in a standardised format.

This helps us (and others!) understand the system.

There are many different UML diagrams, allowing us to model

designs from many different viewpoints. Roughly, there are

(9)

Representing classes in UML diagrams

public class FancyMixedCounter {

private static int sharedCounterValue = 0;

private int counterValue = 0;

public FancyMixedCounter(int startValue) { counterValue = startValue;

}

public FancyMixedCounter(int startValue,  int sharedStartValue) { counterValue = startValue;

sharedCounterValue = sharedStartValue;

}

public int count() {

sharedCounterValue++;

return ++counterValue;

}

public int getInstanceCount() { return counterValue;

}

public static int getSharedCount() { return sharedCounterValue;

}    

Three compartments: name, attribute, and operation

Attribute compartment takes class and instance variables

Operation compartment takes class and instance methods

Types of variables, parameters and return values are specified

Constructors are preceded by <<create>>

Underlined members are static

Can add visibility (public/private) by putting a “+” or a “–” in front of the member (not shown here)

(10)

Technical note: Turning Java classes into UML class diagrams in ArgoUML

File -> Import sources…

Select .java file or directory (tree) with .java files in it

Uncheck the box “Minimise Class icons in diagrams” on the right

Hit “Open”

On the left hand panel in the main window, click on

“untitledModel”

A sub-tree will open

Click on any of the classes (small yellow icons with three

compartments) and the class diagram will appear

(11)

Technical note: Generating Java source code from UML diagrams in ArgoUML

In the class diagram, select the classes you want to generate code for (skip this step if you want to generate code for all classes in the diagram)

Select Generation -> Generate selected classes … or Generation -

> Generate all classes… as required

A small window pops open which lets you select in which languages you want to generate the code of each class

Select an output directory and click on “Generate”

Note: In Java, most primitive data types (int, double, boolean) are also available as classes (Integer, Double, Boolean). When you construct UML class diagrams, ArgoUML will by default offer you UML data types, which translate into the latter class types

(12)

Review

The OO approach is based on modeling the real world using interacting objects.

OO design is a process of determining what the stakeholders require, designing a set of classes with objects which will meet these requirements, implementing, and delivering.

The statements in a class define what its objects remember and what they can do (the messages they can understand), that is, they define

Instance variables, class variables, instance methods, and class methods

A UML class diagram shows the “bare bones” of an OO system design.

It need not show all classes! (A diagram should not have irrelevant information.)

Can use tools to generate UML from actual languages such as Java, and

(13)

Review questions

If a method is declared as static, does its return value have to be stored in a class variable?

Can an instance method set the value of a class variable in its body?

If I have several objects of the same class, how many copies of its class variables exist in memory?

If I have several objects of the same class, how many copies of its instance variables exist in memory?

Can a class method read the value of an instance variable?

How can we invoke a class method?

How do UML class diagrams describe the members of a class? What do class variables look like in UML? What about instance variables? What about constructors?

Referensi

Dokumen terkait

To be considered Islamic, all kinds of positive legislation constitutions, statutes, regulations, treaties, Customs, and case law must be in accordance with the principles of Abstract