You can't. You can only start an application with "java Subclass", if
Subclass has a main method.
There seems to be a fundamental misunderstanding.
Only the simple examples in text-books have 1 single class. All real-world
Java applications have many more than 1 class, typically hundreds of
classes.
The classes interact in many ways. For example: An object of one class calls
methods from objects of other classes. To start the whole application you
need only one starting point. This starting point is the main method in one
class of your choice. There is no need for more starting points, and this is
why all the other classes don't have a main method.

Signature
"TFritsch$t-online:de".replace(':','.').replace('$','@')
Th is my Subclass java program after adding the main method statement,
public static void main(String[] args) {
*****************************************************************************
public class DrawableRect extends Rect {
/** The DrawableRect constructor just invokes the Rect()
constructor */
public static void main(String[] args) {
public DrawableRect(int x1, int y1, int x2, int y2) {
super(x1,y1,x2,y2); }
/** This is the new method defined by DrawableRect */
public void draw(java.awt.Graphics g) {
g.drawRect(x1, y1, (x2 - x1), (y2-y1));};
}
}
*****************************************************************************************
command: javac DrawableRect.java, returned an error messgae:
Illegal start of expression on public DrawableRect(int x1, int y1, int
x2, int y2) { super(x1,y1,x2,y2); }
I know not every java file needs to have a main method. Here I want to
run DrawableRect, which is a subclass. The error message seems to mean
I can not start a subclass.
Do we have to start from Rect.java in order to run DrawableRect.java?
TIA,
Jeffrey
Andrew Thompson - 01 Nov 2005 08:54 GMT
> Th is my Subclass java program after adding the main method statement,
I can see the immediate problem. The code was 'improperly nested'.
Have a look over this variant..
<sscce>
public class DrawableRect extends Rect {
/** The DrawableRect constructor just invokes the
Rect(args) constructor */
public DrawableRect(int x1, int y1, int x2, int y2) {
super(x1,y1,x2,y2);
}
/** This is the new method defined by DrawableRect */
public void draw(java.awt.Graphics g) {
g.drawRect(x1, y1, (x2 - x1), (y2-y1));
}
public static void main(String[] args) {
// the first version had the definition
// of the constructor defined inside the
// 'main' method.
DrawableRect drawableRect =
new DrawableRect(0,0,20,20);
}
}
class Rect {
int x1, y1, x2, y2;
Rect(int x1, int y1, int x2, int y2){
}
}
</sscce>
Note also that this is compilable code, as copy pasted.
When asking questions realted to code (even code that
will not compile) it is often useful to supply an
SSCCE, which is the next best thing.
Check here for more details..
<http://www.physci.org/codes/sscce.jsp>
HTH
pit.grinja@gmx.de - 01 Nov 2005 09:15 GMT
Hi cjeff,
> Th is my Subclass java program after adding the main method statement,
> public static void main(String[] args) {
[quoted text clipped - 6 lines]
>
> public DrawableRect(int x1, int y1, int x2, int y2) {
---> You have placed a method in a method! This is not allowed!...
> super(x1,y1,x2,y2); }
> /** This is the new method defined by DrawableRect */
[quoted text clipped - 7 lines]
> Illegal start of expression on public DrawableRect(int x1, int y1, int
> x2, int y2) { super(x1,y1,x2,y2); }
... and the compiler tells you that your expression (it means your
method declaration) starts at a point in the source where it is not
allowed.
> I know not every java file needs to have a main method. Here I want to
> run DrawableRect, which is a subclass. The error message seems to mean
> I can not start a subclass.
No. You think that what what you want can´t be done, but in fact, it
is not even understood.
What is "starting a subclass"? You can instantiate a subclass (provided
it is not abstract, but lets ignore that for a moment), but of course
only if the source for the class is syntactically/semantically correct
(which is not the case for your example). Change your "DrawableRect"
class as outlined below:
public class DrawableRect extends Rect{
public DrawableRect(int x1,int y1,int x2,int y2){
super(x1,y1,x2,y2)
public static void main(String[] args){
DrawableRect drawableRect = new DrawableRect(10,10,20,20);
}
}
> Do we have to start from Rect.java in order to run DrawableRect.java?
How would you do that? What commands would be necessary to "start from
Rect.java to run DrawableRect.java" ?
When a java program is running, there will be objects generated, doing
some stuff, and when they are no longer needed, they will disappear.
But you do you start this "chain of commands" or better "net of
commands"? What are the first commands that are executed by a java
program? How do you tell the java runtime which class should be used to
generate all the objects that are necessary (and that in turn may
generate new objects)? You have to give the JRE the name of the class.
The JRE will then look for that class. If it is not found, you will get
a "ClassNotFoundException". When the class that you have specified as
an entry point IS found, the JRE will look for a "public static void
main(String[] args)" method of that class. If it is no there, you will
get a "NoSuchMethodError". If the class is there and if it contains a
method with the right signature, then the commands defined in this
method will be executed. In most cases, an instance of the class will
be gerenated, and within the constructor, more objects will generated,
and so on...
HTH
Piet
pit.grinja@gmx.de - 01 Nov 2005 09:22 GMT
Hi,
I just discovered that I forgot the semicolon and the closing curly
braces and the end of the constructor. Maybe i should switch to posting
only sscce´s and not untested stuff.
Piet
Andrew Thompson - 01 Nov 2005 11:43 GMT
> Hi,
> I just discovered that I forgot the semicolon and the closing curly
> braces and the end of the constructor. Maybe i should switch to posting
> only sscce´s and not untested stuff.
I noticed the detached ';', removed it, and forgot
to highlight/mention/document it.
(Shrugs) Personally, I admire people willing to take a
crack at code that has not been compiled (to 'debug by eye'
is an interesting mental exercise), and feel any response
that aims to resolve the problem is good - whether it is
an SSCCE, or vaguely described pseudocode, an untested
single line fix, or a single link to the class/method
that does the job.
[ Just my 2c worth.. ]