This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Single File Source Code: I am able to have more than 1 class in the same file, yet the program runs

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Java 11, and just read in a book, that Single File Source code you cannot use this new feature for a single-file. program with two classes in it
Single File Source Code: I am able to have more than 1 class in the same file, yet the program runs.
Can  anyone explain? Help?
 
Saloon Keeper
Posts: 6619
62
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guessing at what you're referring to: You can have a .java file with more than one class but only one of them may be public. Is that what you're talking about?
 
author & internet detective
Posts: 39771
797
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it was my book, it was because I didn't know that! I just added it to the errata. And whether it was our book or not, I've credited you as the finder:
https://www.selikoff.net/ocp11-1/
 
Dinkar Joshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a pleasant message, yes it was your book. Thanks, I have been scracthing my head , why I was able to add more than 2 classes!!!
 
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DJ: Welcome to the Ranch

I don't know whether things are different on JDK13 but I had no difficulty getting this file to run:-

critchie@localhost directoryXXX]$ java SingleFileDemo.java Campbell Jeanne Liutauras Bear
SingleFile object with field = Campbell
SingleFile object with field = Jeanne
SingleFile object with field = Liutauras
SingleFile object with field = Bear
[critchie@localhost directoryXXX]$ javac -version
javac 13
[critchie@localhost directoryXXX]$ java -version
openjdk version "13" 2019-09-17
OpenJDK Runtime Environment (build 13+33)
OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)

It is a bit of a disappointment if you forget to give it any command‑line arguments And SingleFile is a really misleading class name, I know.
DJ: please show us the code that failed to execute. Also please show us whether there are any XXX.class files with the same name as any of your classes.

          * * * * * * * * * * * * * *

So, I installed JDK11, and tried again:-

/somePath/otherDirectory/java/jdk-11.0.2/bin/java SingleFileDemo.java Campbell Jeanne Liutauras Bear
SingleFile object with field = Campbell
SingleFile object with field = Jeanne
SingleFile object with field = Liutauras
SingleFile object with field = Bear


 
Dinkar Joshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your efforts, however, It seems you mistook what I meant. I am on the same page with you, the code compiles just fine.  The initial discussion was, because in the guide book for Oracle certification, it was stated otherwise. i.e the code should NOT run, This has already been addressed and now been added to the errata as mentioned above.. Follow the link:
https://www.selikoff.net/ocp11-1/
Thanks again.
 
Marshal
Posts: 7351
498
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:I am using Java 11, and just read in a book, that Single File Source code you cannot use this new feature for a single-file. program with two classes in it
Single File Source Code: I am able to have more than 1 class in the same file, yet the program runs.
Can  anyone explain? Help?


For current and future readers, do you mind giving an example what do you mean? I don't understand to be honest.

Campbell's example is good, but you say he misunderstood you... Can you please explain with an example what book says you cannot while you actually can.
 
Liutauras Vilda
Marshal
Posts: 7351
498
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's regarding source code filenames and class definitions there are 2 (at least) things to understand.

If the top-level class in the source code is defined as public, class name MUST match the filename it is defined at (Something.java). That suggests one thing right away - you can have defined at most 1 top-level public class within singular source code file as well as you can have optional non public top-level classes.

If the top-level class in the source code isn't public (meaning it is package private, because top-level class can have either public or default visibility only), filename of the source code and the class name can be different, and same as above, you can have them multiple defined.
 
Dinkar Joshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be a lot of confusion, let me elaborate: The below excerpt is from the OCP guide, which I am using to study for the certification. I have marked the line bold which was the source of confusion. This has been identified as an errata already.



Running a Program in One Line
Starting in Java 11, you can run a program without compiling it first—
well, without typing the javac command that is. Let’s create a new class:
public class SingleFileZoo {
public static void main(String[] args) {
System.out.println("Single file: " + args[0]);
}
}
We can run our SingleFileZoo example without actually having to
compile it.
java SingleFileZoo.java Cleveland
Notice how this command passes the name of the Java file. When we
compiled earlier, we wrote java Zoo. When running it as a one-liner, we
write java SingleFileZoo.java. This is a key difference. After you first
compiled with javac, you then passed the java command the name of the
class. When running it directly, you pass the java command the name of
the file. This feature is called launching single-file source-code programs.
The name cleverly tells you that it can be used only if your program is one
file. This means if your program has two .java files, you still need to use
javac. By contrast, you cannot use this new feature for a single-file
program with two classes in it.
In fact, you can’t refer to any .class files
that didn’t come with the JDK.

Hope this helps: Much Ado about nothing.
 
Liutauras Vilda
Marshal
Posts: 7351
498
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so what I was writing it was different thing, still valid though.

However, what Campbell gave as an example he was right I think demonstrating that it actually works. And so Jeanne added to errata.
I personally know little about Java 11 features, about this in particular I heard, but never tried myself, so can't comment much on that.

But now that you explained all the story in more details, somebody might will expand more on this.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is, you can't use two XXX.java files; that is why it is called single file execution. I shall separate my two classes into two source files:-

pluma SingleFileDemo.java SingleFile.java &
...
/somePath/otherDirectory/jdk-11.0.2/bin/java SingleFileDemo.java Campbell Jeanne Liutauras Bear
SingleFileDemo.java:7: error: cannot find symbol
           new SingleFile(s).run();
               ^
 symbol:   class SingleFile
 location: class SingleFileDemo
1 error
error: compilation failed

I got the same error when I removed the path to JDK11 and used my default (Java13).
 
Rancher
Posts: 3542
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be a proper place to report errata on errata 8)

The link

https://www.selikoff.net/ocp11-1/

says:

In teh fourth paragraph,

 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:. . . We can run our SingleFileZoo example without actually having to compile it. . . .

The java command probably both compiles the XYZ.java file and executes the compiled bytecode. But it doesn't store an XYZ.class file anywhere.

This feature is called launching single-file source-code programs. . . .if your program has two .java files, you still need to use javac. . . .

Yes, that sounds correct I said so on Friday.
 
Campbell Ritchie
Marshal
Posts: 67050
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:. . . you cannot use this new feature for a single-file program with two classes in it. . . .

Is that what you are saying yourself, or a quote from the book? Whichever, it isn't correct.
 
Jeanne Boyarsky
author & internet detective
Posts: 39771
797
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mikalai Zaikin wrote:It might be a proper place to report errata on errata 8)

The link

https://www.selikoff.net/ocp11-1/

says:

In teh fourth paragraph,


Fixed. I don't put the same care into proofing the errata list as the book
 
Jeanne Boyarsky
author & internet detective
Posts: 39771
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Dinkar Joshi wrote:. . . you cannot use this new feature for a single-file program with two classes in it. . . .

Is that what you are saying yourself, or a quote from the book? Whichever, it isn't correct.


That was the quote from my book. I misunderstood the feature when I wrote it. It's in the errata list (thanks to the OP)
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!