• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Clarification

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is the code in Zip.java:

public class Zip {
public void go() {
System.out.println("Zip");
}
}

class Test {
public static void main(String[] args) {
System.out.println("Test");
Zip z = new Zip();
z.go();
}
}

Can anybody explain the output?
Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand your question. It looks like a straightforward class definition. Is there a question about why it outputs what it does?
 
NS Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question:
What is the output of this program?

If you think the output is:
Zip
Test

Think again.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you think Zip would be printed first?

Test is printed before go is called.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I think u have to think again:
the output is as obvious as it looks:
Test
Zip

regards,
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no "main" method available in the Zip class and its "Public". Also, it can't find the main method to execute, hence the exception.
[ May 22, 2006: Message edited by: Sai Charan ]
 
NS Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that was a typo, I meant the following:
Test
Zip


Charan how about the following:

class Zip {
public void go() {
System.out.println("Zip");
}
}

class Test {
public static void main(String[] args) {
System.out.println("Test");
Zip z = new Zip();
z.go();
}
}
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why would run this as java Zip ???
why don't u run it as: java Test and hence no exception !!

regards,
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the output is still:

Test
Zip

regards,
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sai Charan:
There is no "main" method available in the Zip class and its "Public". Also, it can't find the main method to execute, hence the exception.

[ May 22, 2006: Message edited by: Sai Charan ]



Since Test is also a top-level class, a .class file is created for it.

If you use the command line



you will get

Test
Zip
 
Sai Charan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Lynn. (It depnds on what is the java file name that he is trying to save as)
[ May 22, 2006: Message edited by: Sai Charan ]
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
he said that the java file name was Zip.java

and it doesn't depend on that. it depends on which .class you run.

regards,
 
NS Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, I am using textpad to compile and run (No command prompt).

file name Test.java:
class Zip {
public void go() {
System.out.println("Zip");
}
}

public class Test {
public static void main(String[] args) {
System.out.println("Test");
Zip z = new Zip();
z.go();
}
}

1. When I compile and run the above program it prints:
Test
Zip

2. When I make both classes default, compile and run it still prints:
Test
Zip

3. If I make Zip class public and Test class default, change the name of the file to Zip.java, compile and run it throws java.lang.NoSuchMethodError exception.

4. Now if I make both classes default again, compile and run it throws the same exception java.lang.NoSuchMethodError.

Now the question is:
Program contents are same in scenarios 2 and 4 then why different output?

Thanks
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is a factor of the IDE. It is most likely trying to use

java Zip

since you have named the class Zip.
[ May 22, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is happening here
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using Notepad and a command line. I haven't found the explanation yet, but it has to do with the file name.
If I add a step #5 and rename the file back to Test.java, then it will compile and run just fine without any NoSuchMethod error.

Edit...
The JRE looks at the class you invoked the java command on and in your step 2 the class name is 'Test' and in 'Test.class' there is a main method so execution can continue. In step 4 the file name is now 'Zip.java' so the JRE looks in 'Zip.class' and doesn't find the main method which triggers the NoSuchMethod exception.

I think this is right, but please correct me if I'm wrong.
[ May 23, 2006: Message edited by: Tony Thompson ]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However you run it ...
it will always start with test.zip
so in any case output will be :

Test
Zip


i dont understand y there s so much confusion here??
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output depends on how you r executing it:

if you run as : java Zip ;it with throw NoSuchMethodError, because there is no main() in class Zip.

but you run as: java Test and you will get output-
Test
Zip.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic