• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a=b.java

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

Q7 in K&B book says

Given two files:
a=b.java
c_d.class

Are in the current directory, which command-line invocation(s) could complete without error.

A. java -Da=b c_d
B. java -D a=b c_d
C. javac -Da=b c_d
D. javac -D a=b c_d

Answer is A. I would have gone for this as there is nothing that says a=b.java can't be a class name in the options. But is the question not wrong? You can't have = in a class name.....

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A file name is required to match the name of a class or instance only if that class or instance is top-level and public.

So if the file a=b.java does not contain a top-level public class or interface definition, it will work fine. (Note that you would compile using the file name, but then run using the non-public class name.)
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

I couldn't understand your explanation. Would you pls. support your reasoning with the corresponding code sample?

As I understand a=b is not a valid identifier and hence cannot be used as a class name. The following code won't compile:



I would appreciate more clarifications on this topic.

---------------
Ravinder Singh
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops.. sorry for typos.
here is the correct code sample:

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The commands might not be doing what you think. In reverse order, because A is correct.

D. javac -D a=b c_d
C. javac -Da=b c_d

C and D are wrong because javac has no command line parameter "-D". On both accounts it will complain with a "javac: invalid flag" error (and show help).

B. java -D a=b c_d

This isn't going to work for a number of reasons. First, you can ignore the -D because it doesn't do anything in this case (see following explanation). As you noted, you can't name a class with an = sign. The file won't compile and if you name the class something else inside the file, then they are kept at the package protection level and won't be found by the java classloader.

A. java -Da=b c_d

This works because it sets a system property named "a" to the value "b". It then looks for and loads the class named "c_d" from the classpath.
 
Peter MacMillan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate further, suppose that the content of a=b.java is:



When you compile it with javac a=b.java, it will produce the file c_d.class

In order to launch this class (and any class, for that matter) you have to pass the name of the class and not the source file.

so java c_d will launch the c_d class.

The question has a "smoke and mirror" gotcha with the -Da=b part. All that does, as I mentioned, is set a system property named "a" to the value "b". The -D a=b one does nothing and breaks the launch because it won't find a class named a=b
 
Ravinder Singh
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Peter for your explanation. I've understood the concept now.

---------------
Ravinder Singh
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic