Inner Classes & MultiThreading in JAVA
Inner Classes & MultiThreading in JAVA
9/3/2012
9/3/2012
Inner Class
Inner classes are classes defined within other classes. The class that includes the inner class is called the outer class
9/3/2012
Inner Class
same way that the instance variables and methods of the outer
class are members
If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class.
9/3/2012
Structure
public class Outer {
TYPES
There are four categories of inner classes in Java:
9/3/2012
Methods of a non-static inner class can directly refer to any member of any enclosing class (including private members).
9/3/2012
9/3/2012
<InnerClassName>(arguments);
9/3/2012
Example
InnerExample variables; methods();
Example
//A named inner class is used. // This is used to show that it can access non-local variables in the enclosing object. public class InnerExample { static String msg= "Welcome"; public static void main(String[] arg) { class InnerClass { public void doWork() { System.out.println(msg); } } InnerClass i = new InnerClass(); i.doWork(); }
9/3/2012
11
// Access inner class from outside public class InnerExample2 { public static void main(String[] args) { OuterClass outer = new OuterClass(); outer.new InnerClass().welcome(); } } class OuterClass { public class InnerClass { public void welcome() { System.out.println("Welcome from InnerClass()"); } } }
9/3/2012
12
Access
members
Within the definition of a method of an inner class: It is legal to reference a private instance variable of the outer class It is legal to invoke a private method of the outer class The inner class has a hidden reference to the outer class
9/3/2012
13
Cont..
9/3/2012
14
ClassName.class
Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more)
.class files
Such as ClassName.class and ClassName$InnerClassName.class
9/3/2012
15
The rules are the same as before, but the names get longer
Given class A, which has public inner class B, which has public inner class C, then the following is valid: A aObject = new A(); A.B bObject = aObject.new B();
9/3/2012
16
Advantages
They can make the outer class more self-contained since they
9/3/2012
17
Static Class
9/3/2012
18
Inner Class
We define an inner class within a top-level class An inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
InnerClass();
1.
Local
2.
Member
3.
Nested
Local Class
Local classes are the same as local variables, in the sense that
Once you declare a class within a block, it can be instantiated as many times as you wish within that block.
Member Class
You can use member classes anywhere within the body of the
containing class.
The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.
When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal
top-level class
a static modifier.
A nested top-level class is just like any other top-level class except that it is declared within another class or interface.
Nested classes are divided into two categories: static and nonstatic. Nested classes that are declared static are simply called
Static nested classes are accessed using the enclosing class name:
Outer Class. Static Nested Class For example, to create an object for the static nested class, use this syntax:
Class.StaticNestedClass();
As with class methods and variables, a static nested class is associated with its outer class.
And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class it can use them only through an object reference.
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
Multithreading
Multithreading
A thread is part of process in execution Two or more threads executed concurrently is multithreading
Advantage of Multithreading
Thread in JAVA
t.Start()
Wait() Notify()
t.sleep(1000)
Creating a Thread
Two ways
Runnable interface
System.out.println("Child interrupted.");
} System.out.println("Exiting child thread.");
}
}
9/3/2012 37
class ThreadDemo { public static void main(String args[]) { new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i);
Thread.sleep(1000);
} } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting.");}
Output
Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting.
Thread.sleep(500);
} } catch (InterruptedException e) { System.out.println("Child interrupted."); }
class ExtendThread { public static void main(String args[]) { new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); } } catch (InterruptedException e) {
Multiple Threading
class NewThread implements Runnable { String name;
Thread t;
NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t);
t.start();
} public void run() {
try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) {
System.out.println(name + "Interrupted");
} System.out.println(name + " exiting."); } }
class MultiThreadDemo { public static void main(String args[]) { new NewThread("One"); // start threads new NewThread("Two"); new NewThread("Three"); try {
Thread.sleep(10000);
} catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Main thread exiting."); } }
Output
New thread: Thread[One,5,main] New thread: Thread[Two,5,main] Two: 3 One: 2
Three: 2
Two: 2 One: 1 Three: 1 Two: 1 One exiting. Two exiting. Three exiting. Main thread exiting.
isAlive()
Join()
Final void join()
Thread Priority
MAX_PRIORITY
9/3/2012
50
Introduction
Beans are classes written in the Java programming language conforming to a particular convention.
They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects.
9/3/2012
51
Introduction
A Beans methods are no different than Java methods, and can be called from other Beans or a scripting environment Beans can be used with both builder tools, or manually manipulated by text tool through programmatic interfaces. Compact and Easy Portable Leverages the Strengths of the Java Platform Flexible Build-Time Component Editors Multithreading Security
9/3/2012 52
Convention
has a 0-argument constructor The class must have a public default constructor. This allows easy instantiation within editing and activation frameworks.
9/3/2012
53
package sunw.demo.simple;
Example
import java.awt.*;
import java.io.Serializable; public class SimpleBean extends Canvas implements Serializable
{color = newColor;
repaint(); }
{
private Color color = Color.green; public SimpleBean(){ setSize(60,40); setBackground(Color.red); } }
9/3/2012 54
http://www.mediafire.com/?4hn81bgt83m29dg
9/3/2012
55
Builder tool
A builder tool maintains Beans in a palette or toolbox. The user can select a component from the toolbox and place it into a container, choosing its size and position.
application developer.
The application development tool does that. This is accomplished through a set of standard interfaces provided by the component environment that allow the components to publish, or expose, their properties.
9/3/2012 56
BeanBox
When started, the BeanBox displays three windows: The BeanBox, ToolBox, and the Properties sheet.
9/3/2012
57
Loading bean
When the BeanBox is started, it automatically loads all the Beans it finds within the JAR files contained in the \BDK1.1\jars
directory.
You can also load Beans Using the File|LoadJar menu. The ToolBox contains the Beans available for use by the BeanBox.
Click on the bean name in the ToolBox. The cursor will change to
a crosshair.
Click within the BeanBox. The Bean will appear and will be selected.
9/3/2012 59
name.
These properties are usually read and written by calling methods on the Bean specifically created for that purpose.
9/3/2012
60
Edit Properties
9/3/2012
61
Beans use events to communicate with other Beans. A Bean that wants to receive events (a listener Bean) registers its interest with the Bean that fires the event (a source Bean).
Builder tools can examine a Bean and determine which events that Bean can fire (send) and which it can handle (receive).
To see which events a Bean can send, choose the Edit|Events BeanBox menu item.
A list of events, grouped by interface, will be displayed. Under each interface group is a list of event methods. These are all inherited from Canvas.
9/3/2012 62
9/3/2012
63
You will label the buttons "start" and "stop"; Make the start button, when pressed, invoke the Juggler beans start method.
Make the stop button, when pressed, invoke the Juggler beans stop method.
9/3/2012
64
A Bean persists by having its properties, fields, and state saved and restored to and from storage.
When a Bean instance is serialized, it is converted into a data stream and written to storage.
interface
9/3/2012
65
You can generate a Bean introspection report by choosing the the Edit|Report menu item.
The report lists Bean events, properties, and methods, and their characteristics.
standard output.
9/3/2012
66
Use the default JAR file and applet name for this example. Press
the OK button.
9/3/2012
67
Write once, run anywhere. The configuration settings of a Bean can be saved in a persistent storage and can be restored at a later time.
A class with a null constructor is subject to being instantiated in an invalid state. The developer might not realize that. The
Interface AppletInitializer
Description Methods in this interface are used to initialize Beans that are also applets. This interface allows the designer to specify information about the events, methods and properties of a Bean. This interface allows the designer to provide a graphical user interface through which a bean may be configured. Methods in this interface determine if a bean is executing in design mode. A method in this interface is invoked when an exception has occurred. A method in this interface is invoked when a bound property is changed. Objects that implement this interface allow the designer to change and display property values. A method in this interface is invoked when a Constrained property is changed. Methods in this interface allow a bean to execute in environments where GUI is not available.
9/3/2012 69
BeanInfos
Customizer
Visibility
References
9/3/2012
70
Thank yu!
9/3/2012
71