Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many ways of creating a class?

 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys! I did it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic