Object-Oriented Programming: Static Methods & Variables, Schemes and Mind Maps of Object Oriented Programming

Cannot make a static reference to the non- static field loc. Ewan Klein. Object-Oriented Programming: Static Methods & Variables ...

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

vernon
vernon 🇺🇸

4.8

(5)

216 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
. . . . . .
Static Methods Static Variables Admin
.
.
.
.
.
.
.
Object-Oriented Programming:
Static Methods & Variables
Ewan Klein
Inf1 :: 2008/09
Ewan Klein Object-Oriented Programming: Static Methods & Variables
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Object-Oriented Programming: Static Methods & Variables and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity!

......

Object-Oriented Programming:

Static Methods & Variables

Ewan Klein

Inf1 :: 2008/

...... ..^1.^ Static Methods ..^2.^ Static Variables ..^3.^ Admin

......

.. What are Static Methods?

..^1.^ What are static methods?

..^2.^ You’ve already seen them.

......

.. What are Static Methods?

..^1.^ What are static methods?

..^2.^ You’ve already seen them.

Calling a method from Maths

..

.

...

int randomNum = (int)( Math.random() * 5)

......

.. Static vs. Non-Static

All ‘regular’ methods are non-static. A static method can be run without instantiating the class. Also called class methods. .Calling a Static Method .. .

...

Math.max(7, 22);. . Calling a Non-Static Method .. .

...

Cell c = new Cell(); c.move(6);

......

.. Classes with Static Methods, 1

Classes with static methods often not meant to be instantiated. How to stop instantiation? ..^1.^ Make the class abstract. ..^2.^ Make the constructor private. .Private Constructor ..

public class Sim { private Sim() { } }

......

.. Classes with Static Methods, 2

.Static Method in.^ Sim

public static Sim getInstance() { if (instance == null) { instance = new Sim(); } return instance; } . Calling the Method ..

public class AnimalCell extends BaseCell { Sim world = Sim.getInstance(); ... }

......

.. Classes with Static Methods, 3

Classes with static methods may be instantiated. And static and non-static methods can be mixed. . .Mixed methods.

public class Cell1 { public static void main(String[] args) { Cell1 c = new Cell1(); c.hello(); } public String hello() { return ”I’m a cell!”; } }

......

.. Static Methods and Instance Variables, 1

A static method cannot use an instance variable. Think about it. .Mixed methods ..

public class Cell int loc = 0; public static void main(String[] args) System.out.println(”I’m at: ” + loc ); // Wrong! public int getLoc() return loc ; // OK

......

.. Static Methods and Instance Variables, 2

Output ..

Exception in thread ”main” java.lang.Error: Unresolved com- pilation problem: Cannot make a static reference to the non- static field loc

......

.. Static Methods calling Non-Static Methods, 2

.Does Work.

public class Cell3 { int loc = 0; public static void main(String[] args) { Cell3 c = new Cell3(); System.out.println(”I’m at: ” + c.getLoc()); } public int getLoc() { return loc; } }

......

.. Static Variables, 1

Static variable: one value per class, not one value per instance. Also called: static field, class variable

......

.. Static Variables, 2

.How many prints?

...

public class Lithograph { static int printsMade = 0; private int copyNo; public static void main(String[] args) { ArrayList prints = new ArrayList(); for (int i = 0; i < 6; i++) { prints.add(new Lithograph()); } for (Lithograph l : prints) { l.howMany(); } } public Lithograph() { printsMade++; copyNo = printsMade; } public void howMany() { System.out.printf(”Copy %d of %d%n”, copyNo, printsMade); } } Ewan Klein Object-Oriented Programming: Static Methods & Variables

......

.. Static Variables, 3

.Output ..

Copy 1 of 6 Copy 2 of 6 Copy 3 of 6 Copy 4 of 6 Copy 5 of 6 Copy 6 of 6