Presentation is loading. Please wait.

Presentation is loading. Please wait.

Feb 18, 2000 Intermediate Containers. Global, Local and Static zGlobal/local are terms for variables in a program written in procedural-based language.

Similar presentations


Presentation on theme: "Feb 18, 2000 Intermediate Containers. Global, Local and Static zGlobal/local are terms for variables in a program written in procedural-based language."— Presentation transcript:

1 Feb 18, 2000 Intermediate Containers

2 Global, Local and Static zGlobal/local are terms for variables in a program written in procedural-based language. (such as C) zSince we are doing OOP (everything is object), we can not distinguish global/local objects in program level. zHowever, we can say class variable( or fields) are global within an object since they are visible to all methods in the same object. (In that sense, whatever defined in a method is local) Therefore, fields are global in an object level. zStatic keyword is introduced to serve as global, however this should be carefully understood. Recall we can not say which object is global or local.

3 What it means by static zIn OOP world, every object should be instantiated to be used in a program. zjava.lang.Math contains a bunch of mathematics functions. Since we are only interested in particular methods (functions) in Math class, not entire copy (object) of it, it will be waste of space if multiple objects of Math type are created just to serve different objects. zDefined as static, a method can be directly called by other objects whether its enclosing object is created or not. zThe same is true for static fields. Famous examples are System.out, Math.PI, Color.yellow, etc. zStatic fields are global among the objects or the same type.

4 Object Handle and Pointer zThey say there’s no pointers in Java. However, object handles are essentially pointers. zRecall that objects are created in Garbage-collected Heap Area. By creating an object, we only get a handle that points to real object in the Heap. zBy passing an object handle, we simply pass the pointer. Not the object itself!!!

5 Object Handls class Game{... Card card = deck.draw(); player[0].getCard(card); … } class SimplePlayer extends Player { Card card[] = new Card[10]; … void getCard(Card card) { card[1] = card; } Array in CardDeck Array in SimplePlayer Card objects in heap object handle

6 Vector Class zMost frequently used Java Classes. zVector is a resizable array. You don’t have to worry about its capacity. It will resize itself. zHowever, Vector can not hold primitive data types. It only holds Objects. zI do not know why they call it as Vector. zGood example of generic handling and upcasting and downcasting.

7 Useful Intermediate Containers zPanel (JPanel) zScroll Pane (JScrollPane) zEditor Pane (JEditorPane) zSplit Pane (JSplitPane) zTool Bar (JToolBar) zTabbed Pane (JTabbedPane)

8

9 import java.awt.*; import java.awt.event.*; import javax.swing.*; class ScrollPaneDemo { ScrollPaneDemo() { JFrame frame = new JFrame("ScollPane"); frame.setSize(200,200); Container pane = frame.getContentPane(); pane.add(makeScrollPane()); frame.setVisible(true); } private Component makeScrollPane() { // Implementation comes in the next slide. } public static void main(String args[]) { new ScrollPaneDemo(); }

10 private Component makeScrollPane() { JPanel panel = new JPanel(); ImageIcon image = new ImageIcon("cab.gif"); panel.add(new JLabel(image)); JScrollPane scroll_pane = new JScrollPane(panel); return scroll_pane; }

11 class ToolBarDemo extends JFrame { ToolBarDemo() {... JToolBar toolBar = new JToolBar(); addButtons(toolBar);... Container pane = getContentPane(); pane.setLayout(new BorderLayout()); pane.add(toolBar, BorderLayout.NORTH); pane.add(scrollPane, BorderLayout.CENTER); } protected void addButtons(JToolBar toolBar) { // See the next slide!!! } public static void main(String args[]) { new ToolBarDemo(); }

12 protected void addButtons(JToolBar toolBar) { JButton button = null; //first button button = new JButton(new ImageIcon("images/left.gif"));... toolBar.add(button); //second button button = new JButton(new ImageIcon("images/middle.gif"));... toolBar.add(button); //third button button = new JButton(new ImageIcon("images/right.gif"));... toolBar.add(button); }


Download ppt "Feb 18, 2000 Intermediate Containers. Global, Local and Static zGlobal/local are terms for variables in a program written in procedural-based language."

Similar presentations


Ads by Google