1 / 11

Creating Instances

Creating Instances. Think of a class as a blueprint. The blueprint isn’t the house. It’s the plans for a house. From that blueprint (or class) you can make many instances(objects) of that house. Let’s say we have our Rectangle class.

Download Presentation

Creating Instances

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Instances

  2. Think of a class as a blueprint. • The blueprint isn’t the house. It’s the plans for a house. • From that blueprint (or class) you can make many instances(objects) of that house.

  3. Let’s say we have our Rectangle class. • We can create multiple “instances” of that class like this: Rectangle box1 = new Rectangle(); • Now we have a Rectangle object named “box1” Rectangle box2 = new Rectangle(); • Now we have a Rectangle object named “box2”

  4. Each Rectangle object (or instance) has access to the Rectangle class’ procedures and data fields. • However, each Rectangle object has its own INSTANCE of those procedures and data fields.

  5. Data (Fields) Data (Fields) -dblLength = 3 -dblWidth = 7 -dblLength = 5 -dblWidth = 10 • Ex: ---------------------------------- ---------------------------------- -setLength( ) -setLength( ) -setWidth( ) -setWidth( ) -getLength( ) -getLength( ) -getWidth( ) -getWidth( ) -calcArea( ) -calcArea( ) Methods That Operate on the Data Methods That Operate on the Data box2 - This variable holds the memory address of this Rectangle object box1 - This variable holds the memory address of this Rectangle object

  6. Instance Variables: • Also known as instance fields. • The variables in each separate instance of the class. • For example, each instance of the Rectangle class has its own dblLength and dblWidth variables.

  7. Instance Methods: • Also known as instance procedures. • The methods in each separate instance of the class. • For Example: • box1  This instance has its own setLength method. • box2  This instance has its own setLength method. • Instance methods do not have the keyword “static” in their method headers.

  8. Main methods usually are written like this: public static void main( ) • Methods in other classes are written like this: public void getLength( ) Why is static there? Why do we not put static there?

  9. Static: • A header declaration that tells Java whether or not multiple instances of a variable or method are allowed. • If a data member(variables and methods) is static, that means that it is the only ONE. • There can not be multiple instances of something if it is static.

  10. For example, if you see this: public static void main( ) • There can only be ONE instance of the main method. It is static. Main ------------------ Main ( )

  11. If you see this: public void getLength( ) • There can be multiple instances of the getLength method. It is NOT static. box1 ------------------ getLength ( ) getWidth( ) box2 ------------------ getLength ( ) getWidth( ) box3 ------------------ getLength ( ) getWidth( )

More Related