Tuesday
Java ProgrammingObjects of Java
Class Instantiation

The administer of making stuff from a class is called instantiation. An object is an instance of a class. The object is constructed using the class as a blueprint and is a concrete instance of the abstraction that the class represents. An object must be made before it can be used in a program. In Java, stuff are manipulated through object references (also called reference principles or simply references). The administer of making stuff usually involves the following steps:
- Declaration of a variable to store the object reference.
This involves declaring a reference variable of the appropriate class to store the reference to the object.// Declaration of two reference variables that will denote
// two evident stuff, namely two stacks of font, respectively.CharStack stack1, stack2;
- Making an object.
This involves using the new operator in conjunction with a call to a constructor, to start an instance of the class.// Start two evident stacks of chars.stack1 = new CharStack(10); // Stack length: 10 charsstack2 = new CharStack(5); // Stack length: 5 chars
The new operator returns a reference to a new instance of the CharStack class. This reference can be assigned to a reference variable of the appropriate class.
Each object has a unique identity and has its own copy of the fields declared in the class definition. The two stacks, denoted by stack1 and stack2, will have their own stackArray and topOfStack fields.
The purpose of the constructor call on the right side of the new operator is to initialize the newly made object. In this particular case, for each new CharStack instance made using the new operator, the constructor makes an array of font. The length of this array is given by the value of the argument to the constructor. The constructor also initializes the topOfStack field.
The declaration and the instantiation can also be combined:
CharStack stack1 = new CharStack(10),
stack2 = new CharStack(5);

Figure 1.2 shows the UML notation for stuff. The graphical representation of an object is very similar to that of a class. Figure 1.2 shows the canonical notation, everywhere the name of the reference variable denoting the object is prefixed to the class name with a colon ‘:’. If the name of the reference variable is omitted, as in Figure 1.2b, this denotes an anonymous object. In view of the fact that stuff in Java do not have names, but are denoted by references, a more elaborate notation is shown in Figure 1.2c, everywhere stuff representing references of CharStack class explicitly refer to CharStack stuff. In most cases, the more compact notation will suffice.
Figure 1.2. UML Notation for Stuff
Object References
A reference provides a handle to an object that is made and stored in memory. In Java, stuff can only be manipulated via references, which can be stored in variables. An object can have several references, often called its aliases. The object can be manipulated via any one of its aliases.
// Start two evident stacks of chars.
CharStack stackA = new CharStack(12); // Stack length: 12 chars
CharStack stackB = new CharStack(6); // Stack length: 6 chars
stackB = stackA; // (1) aliases after assignment
// Stack previously referenced by stackB can now be garbage collected.

Figure 1.3. Aliases
Two stacks are made in the code above. Before the assignment at (1), the situation is as depicted in Figure 1.3a. After the assignment at (1), reference variables stackA and stackB will denote the same stack, as depicted in Figure 1.3b. Reference variables stackA and stackB are aliases after the assignment, as they refer to the same object. What happens to the stack object that was denoted by the reference variable stackB before the assignment? When stuff are no longer in use, their memory is, if necessary, reclaimed and reallocated for other stuff. This is called automatic garbage collection. Garbage collection in Java is taken care of by the runtime system.
Post Tags: Basic Java, Basic of Java, Basic of Java Programming, Java for Beginner, Java Programming, Learn Java
Related Posts
- The Basics of Multiple Threads in Java
- Basics of Java Programming
- Inheritance and Aggregation in Java Language
- More About Multithreading in Java
- Refactoring the MySQL Abstraction Class with the Active Record Pattern
- Java Instance and Static Members
- Using XSL Formatting Objects
- Tomcat Performance Tuning
- The Child And The Computer
- Images And Flash In Your Website
Popular
- iPhone or iPod Touch, Which One Should You Choose? - 47,761 views
- Introduction to Facebook - 26,655 views
- 7 Top Twitter Topic Trackers - 16,375 views
- Introduction of Hacking Methodology - 15,051 views
- Google Hacking Keywords - 11,716 views
- LG BD390 Wi-Fi Blu-ray Player Review: So Packed You’ll Forget About Blu [Review] - 10,094 views
- Complete Guide to Web Site Marketing - 9,774 views
- BlackBerry Curve 3G: A Familiar, Powerful Phone - 9,774 views
- Microsoft’s Zune HD Wiill Have OLED, HD Radio - 9,300 views
- Photo Album and Printing Services - 8,953 views
Featured Articles
- The Elements of the Facebook Platform
- Introducing the Facebook Platform
- Social Skills Today Are Being Lost
- GibBook – Gibraltar Own Social Network Site
- 7 Deadly Twitter Sins
- Introduction to Facebook
- Google’s Wave: Many Online Apps in One Tool
- App Mapping War Casualties Debuts for Memorial Day Weekend
- Google@Omgili Mashes Traditional Web Search With Social Buzz
- Capital Factory: Austin-Based Incubator (RWS Interview)
Recent Posts
- Tweet via something
- Lightroom Review And Other Phot
- Several Words About The Advantages Of Tablet PCs
- Dissimilar Positive Features Of Table Computers
- Digi Link Doctor
- Why You Need Professional SEO Software
- Automated SEO Tools For Online Business
- How To Choose The Best Tablet PC
- Tablet PC – The Latest Computer Technology
- Tablet PCs Compared To Laptops