The following program was written a while ago
to demonstrate object creation process in java
The console output is shown below
to demonstrate object creation process in java
////////////////////////////////
// Simple.java
//
// A Java program to demonstrate hw to
// instantiate an Object using
// A) on heap using new
// B) Java Beans framework
// C) Reflection
//
//
// usage:-
// Javac Simple.java
// java Simple
//
//
//
import java.lang.*;
import java.lang.*;
import java.beans.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static java.lang.System.out;
//////////////////////////////
//
// A Simple Java Bean ( class )
//
//
public class Simple
{
///////////////////////
//
// An Instance variable...
private String message;
/////////////////////////////////
//
// CTOR
//
public Simple( ){
message = null;
}
/////////////////////////
// Java uses patterns to simulate properties...!
//
public String getmessage() { return message; }
public void setmessage(String pmessage) {
message = pmessage;
}
//////////////////////////////////
// A test driver...
//
public static void main( String [] args ) throws IOException,
ClassNotFoundException,
InstantiationException,
Exception
{
///////////////////////////////////
//
// Instantiate using new
//
//
Simple sm = new Simple();
sm.setmessage("Hello world...");
System.out.println(sm.getmessage());
//////////////////////////////////////////////
// Instantiate using Java Beans
//
//
Simple sm2 = (Simple)Beans.instantiate(
Simple.class.getClass().getClassLoader(),"Simple");
if ( sm2 == null ) {
System.out.println("Failed to load the class....");
return;
}
sm2.setmessage("Hello world...");
System.out.println(sm2.getmessage());
/////////////////////////////////////////////////////
//
//
//
Class c = (Class)Class.forName("Simple");
if ( c == null ) {
System.out.println("Failed to load the class....");
return;
}
Constructor[] ctors = c.getDeclaredConstructors();
Constructor ctor = null;
for (int i = 0; i < ctors.length; i++) {
ctor = ctors[i];
if (ctor.getParameterTypes().length == 0)
break;
}
Simple sm3 = (Simple)ctor.newInstance();
sm3.setmessage("Hello world...");
System.out.println(sm3.getmessage());
}
}
The console output is shown below
D:\Instantiation>javac Simple.java
D:\Instantiation>java Simple
Hello world...
Hello world...
Hello world...
D:\Instantiation>
3 comments:
Yet another first person shooter game this time around, but the first Far Cry was quite good.
So even at young ages and relatively low levels of the sport, goalies will
have their helmets designed with various colors, prints and patterns, shapes or designs, and more.
Hometown Retired households are comprised of retirees, two-thirds of whom are over 65; nonetheless, don't assume that these people don't do much more than hang around
the house and watch TV all day. 99 Mac Mini only ships with 1GB of RAM.
Of course, the layout of the apps is completely customisable, so you can place
your most commonly used apps and widgets within easy reach,
so they can be instantly accessed after unlocking the screen.
Those pain killers and medications hide the problem without dealing
with the true cause. They will check into a purpose built sports complex 8 miles from the
venue of their first match in Rustenburg.
FIFA has stated that a player named as a goalkeeper can only play
in goal and that this rule will be enforced. A far cry from the land of milk and honey you may think.
We have witnessed this first hand in this business.
Here is my homepage Click That Link
I am basically not a programmer and I am comparatively new to Java technology , so I was wondering what all topics should be covered up if i have to start java from the start and has any one
studied or got any info regarding this 6 week java training online course http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate and should we also have knowledge of C language before we further move on to Advance Java topics??
NICE BLOG!!! Thanks for sharing useful information about FNT Softwre Solutions and being one of best Software Training institute in Bangalore we agree that this blog is very useful for the students who are searching for best software courses, I would really like to come back again right here for like wise good articles or blog posts. Thanks for sharing...Java training company bangalore.
Post a Comment