Вы находитесь на странице: 1из 12

Objects, Classes, and Events

this
Each time an object is instantiated, a pointer is created in the stack to the object, which resides in the heap A copy of the pointer is given to anything that instantiates the object using the new keyword When a method that belongs to an object is in scope, a copy of the pointer to the object itself is put in the stack and is available via code using the this keyword.

static
static classes and values exist only as a single instance, unlike objects. Static methods can only access static items, and never use this, since there is no instance as 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 you only need a single item (method, property) across all instances When the JVM is reset, so are static properties

static
What are some good examples weve seen/used this quarter? Example

interface
Interfaces are a contract of public properties and methods A class that implements a given interface must include methods that fulfill the contract The purpose is so that users of a given class can use an interface they need without knowledge of all of the different classes that implement it

interface

Example

Event-Driven programming
How do GUI interfaces such as Windows and OSX differ from procedural interfaces such as a command line? How many events can a user cause? Using Javas awt package, it is possible to write event listeners that perform a given method when a given event occurs

ActionListener interface
This is one of many interfaces used to handle events It declares an actionPerformed() method that takes in an ActionEvent object. You can write classes that implement this interface. This will allow you write your own event handlers.

ActionListener and JButton


To add an event to a JButton, simply use the objects addActionListener method Pass in an instance of your class that implements ActionListener When the button is clicked, the ActionListner objects actionPerformed method will be triggered

Nested Classes
Nested classes are classes that are defined within a class This means you can have a class that does not have its own .java file When compiled, the inner class will have its own .class file created These are used when the class is only useful to the outer class

Nested Classes
Inner Class (aka Member class): a class defined within a class. It has access to all members of the class that defined it. Nested Static Class: An inner class with the static keyword. Everything in it and everything it accesses is static Local Class: A class defined within a block of code of another class. It has access to all members in the class that defined it.

Anonymous Inner Classes


These are defined inside another class and not assigned a reference name Mostly used for writing event handlers Discuss example

Вам также может понравиться