Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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

 
Greenhorn
Posts: 20
  • Likes 1
  • 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: 7386
66
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: 40239
819
Eclipse IDE VI Editor Java
  • Likes 2
  • 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: 20
  • 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: 70629
288
  • 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: 20
  • 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: 7785
536
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: 7785
536
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: 20
  • 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: 7785
536
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: 70629
288
  • 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).
 
Bartender
Posts: 3655
38
  • 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: 70629
288
  • 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: 70629
288
  • 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: 40239
819
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: 40239
819
Eclipse IDE VI Editor Java
  • Likes 1
  • 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)
 
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fully understand this one-line launcher is out of scope of the exam 1z0-819. But I still want to clarify some confusion in case anyone happen to see this post.

Though you can have multiple classes in a single file, but there is a limitation.

From JEP 330: Launch Single-File Source-Code Programs:

JEP330 wrote:In source-file mode, the effect is as if the source file is compiled into memory, and the first class found in the source file is executed.



That is to say, if you have more than one class in a file, it doesn't matter if file name matches class name or if the class is public. It works only if the first class has a main entry point.

For example, the following code works.

But this one doesn't work, because the first class in the file is Class2 which doesn't have a main entry point:
 
Carey Brown
Saloon Keeper
Posts: 7386
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:For example, the following code works.


Note that this is only true if this code is in a file named "Class1.java". If the following code is in a file named "Class1.java" it will not work. So, not only does the first class need to contain main() the class name must also match the file name.
Note that this also works when you add "public".
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote: Note that this is only true if this code is in a file named "Class1.java". If the following code is in a file named "Class1.java" it will not work. So, not only does the first class need to contain main() the class name must also match the file name.


That is not true. The name doesn't matter. You can change the name to whatever you want. For example, you can rename the file to Random.java, it doesn't matter. My quote is directly from JEP 330 and it has the same results on my computer. I guess you might forget to save your file when you launch it. (Just to be sure, you know this post is about the one line launcher for Single-File Source-Code Program, rather than the normal javac/java, right?)

For example, the following code in a file named "Random.java". It works. As I said, it doesn't matter what the file name is or if public or not. They are just irrelevant.




 
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Frank Mi wrote:

Carey Brown wrote: Note that this is only true if this code is in a file named "Class1.java". If the following code is in a file named "Class1.java" it will not work. So, not only does the first class need to contain main() the class name must also match the file name.


That is not true. The name doesn't matter.


Just to make sure you're both on the same page, we're talking about single file source code and executing code directly with the java command like so:

$ java SingleFileSource.java

Note the extension is .java and the command is java, not javac then java.

The former way is what we're talking about and hence Frank is right, the file name does NOT have to match the top level public class name. In single file source code mode, the java command will attempt to execute the first class it finds in the source code and therefore, it needs to have a static void main(String[] args) method.
 
Jeanne Boyarsky
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank,
Have a cow! I did not know that. I have a presentation on unexpected behavior in Java. I'm adding this one next time I give it!

Is it ok if I include your name in the thank yous?
 
Junilu Lacar
Sheriff
Posts: 15929
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At some point, maybe Java 11, the --source command line argument was needed to run the java command in single-file source mode. See this article: https://www.baeldung.com/java-single-file-source-code

This doesn't seem to be the case any more, at least not in Java 14.
 
Jeanne Boyarsky
author & internet detective
Posts: 40239
819
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,
There's two related features here. Using single file source code to run "java Foo.java" never required --source.

There's also a "shebang" option where you put something like this in the beginning of your file. That lets you run it like a shell script. Alternatively you can use --source to run the shebang. file. But to run a regular Java program, you don't need --source


Both a re described in the JEP
 
Frank Mi
Ranch Foreman
Posts: 122
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Is it ok if I include your name in the thank yous?



Yes, of course. It's my honor!
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: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




how can JVM run without class being public ??

or am I missing something ??
 
Campbell Ritchie
Marshal
Posts: 70629
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

R K Singh wrote:. . . how can JVM run without class being public ?? . . .

It is programmed to look for the first class in the file, I think, regardless of its access. If you use the old way of compiling code, with javac and java, there is no requirement for the class with the main() method to be public, either.
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is programmed to look for the first class in the file, I think, regardless of its access. If you use the old way of compiling code, with javac and java, there is no requirement for the class with the main() method to be public, either.



My bad, never tried, and trusted the theory that class has to be public so that jvm may call its public main() method. (Or may be I assumed that class has to be public )

I think it may work if there is only one class in the package

Anyway, thanks for the clarification.
 
Ranch Foreman
Posts: 1796
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any advantage of having multiple classes in a single file instead of multiple files? thanks
 
Campbell Ritchie
Marshal
Posts: 70629
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think, MS?
 
Monica Shiralkar
Ranch Foreman
Posts: 1796
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What do you think, MS?



I have seen that at times and have wondered why were these kept in a single file instead of multiple. To me having 1 file per class looks more readable.
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Campbell Ritchie wrote:What do you think, MS?



I have seen that at times and have wondered why were these kept in a single file instead of multiple. To me having 1 file per class looks more readable.



unless you dont want that class to be accessible by others
 
I want my playground back. Here, I'll give you this tiny ad for it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic