aspose file tools*
The moose likes Beginning Java and the fly likes How many ways of creating a class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many ways of creating a class?" Watch "How many ways of creating a class?" New topic
Author

How many ways of creating a class?

Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
hello all,
I know that there are two approaches to create a class, for example:
class ClassName { declaration, constructor, method }
class AnotherClassName { declaration, constructor, method }
//main
public class classname
{
public static void main(String[] args)
{
xxx xxx = new xx();
xxx.show();
}
}
or
public class ClassName extends ...
{
constructor
class{..}
class{..}
method(..)
public static void main(String[] args)
{..}
}
Can someone please tell me the other ways of creating a class?And which approach is the best and easy to use?
I also have another question about constructor:
Can a constructor extends an object?
e.g. public frameStructure extends JFrame {....}
Is this constructor valid? If it is not, how can I create a frame inside the constructor?
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
There is another way to load class into memory and then invoke one of the constructors using Class class and then using Reflection to execute it.
there is a package called java.lang.Refl;
if you look into it you'll find it very usefull class in there. You wouldn't use this unless you need to load the classes dynamically at runtime.


Val SCJP <BR>going for SCJD
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Val the question is different.
Mindy
Yes you can declare classes in otherways also.
you can create new classes inside another class. these are called innner classes
And you can declare classes in methods
Yes methods also, these class are called local classes and the scope of these classes will be limited to that method, but they can access all the datamembers and method of the outer class.
Another way is to create a class without any name. These are called Anonymous Classes.
It depends on exactly your application needs. if want to create a class for using only once, you can use anonymous classes.
you can create in whatever way you want, provided you follow the rules of road.
Your next question...
Can a constructor extends an object?
Nope you can use the word extends only for classes and interfaces.
You can create a new class (e.g JFrame ) in the constructor also.
Hope this helps
cheers
Siva
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
I also have another question about constructor:
Can a constructor extends an object?
e.g. public frameStructure extends JFrame {....}
Is this constructor valid? If it is not, how can I create a frame inside the constructor?


Notice that you only declare the class to extend something, not the constructor. The call to super() is what constructs the extended object, and it can be passed any parameters that were passed in your class constructor. Whenever you call super() in a constructor, it effectively calls the constructor of the object your class extends.
Hope that helps,
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Mindy Wu
Ranch Hand

Joined: Jan 12, 2001
Posts: 121
Thank you guys! I did it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many ways of creating a class?