Download - Java: Week 7
-
7/30/2019 Java: Week 7
1/12
Objects, Classes, and Events
-
7/30/2019 Java: Week 7
2/12
this
Each time an object is instantiated, apointer is created in the stack to theobject, which resides in the heap
A copy of the pointer is given toanything that instantiates the objectusing the new keyword
When a method that belongs to anobject is in scope, a copy of thepointer to the object itself is put inthe stack and is available via code
using the this keyword.
-
7/30/2019 Java: Week 7
3/12
static
static classes and values exist only as asingle instance, unlike objects.
Static methods can only access static items,
and never use this, since there is no instanceas far as they are concerned.
The data is stored in the method area,which belongs to the JVM.
You can use these in your classes when youonly need a single item (method, property)across all instances
When the JVM is reset, so are static properties
-
7/30/2019 Java: Week 7
4/12
static
What are some good examples weve
seen/used this quarter?
Example
-
7/30/2019 Java: Week 7
5/12
interface
Interfaces are a contract of publicproperties and methods
A class that implements a giveninterface must include methods thatfulfill the contract
The purpose is so that users of a
given class can use an interface theyneed without knowledge of all of thedifferent classes that implement it
-
7/30/2019 Java: Week 7
6/12
interface
Example
-
7/30/2019 Java: Week 7
7/12
Event-Driven programming
How do GUI interfaces such asWindows and OSX differ fromprocedural interfaces such as a
command line? How many events can a user cause?
Using Javas awt package, it is
possible to writeevent listeners
that perform a given method when agiven event occurs
-
7/30/2019 Java: Week 7
8/12
ActionListener interface
This is one of many interfaces used tohandle events
It declares an actionPerformed()method that takes in an ActionEventobject.
You can write classes that implement
this interface. This will allow you writeyour own event handlers.
-
7/30/2019 Java: Week 7
9/12
ActionListener and JButton
To add an event to a JButton, simplyuse the objects addActionListenermethod
Pass in an instance of your class thatimplements ActionListener
When the button is clicked, the
ActionListner objectsactionPerformed method will betriggered
-
7/30/2019 Java: Week 7
10/12
Nested Classes
Nested classes are classes that aredefined within a class
This means you can have a class thatdoes not have its own .java file
When compiled, the inner class willhave its own .class file created
These are used when the class is onlyuseful to the outer class
-
7/30/2019 Java: Week 7
11/12
Nested Classes
Inner Class (aka Member class): a classdefined within a class. It has access to allmembers of the class that defined it.
Nested Static Class: An inner class with thestatic keyword. Everything in it andeverything it accesses is static
Local Class: A class defined within a block
of code of another class. It has access to allmembers in the class that defined it.
-
7/30/2019 Java: Week 7
12/12
Anonymous Inner Classes
These are defined inside anotherclass and not assigned a referencename
Mostly used for writing eventhandlers
Discuss example