Java class files with $ symbol - java

I have a class with name StreamGo. It has 4 more class files with the same name like - StreamGo$1, StreamGo$2 etc. All these class files have different set of code. How do I merge them? And why does these class files get created with $1 and $2 etc?
Please help.

Those class files are automatically generated for anonymous inner classes defined inside StreamGo.java. For example, if you have something like this:
final Runnable runnable = new Runnable() {
public void run() {
// ...
}
};
then you'll have a StreamGo$1.class for the anonymous implementation of Runnable that you've defined here.
Named nested classes will do much the same, except that you'll get their names instead of the number part.

These are compiler-generated classes for anonymous classes that you create in your class.
Since Java bytecode requires a separate .class file for each class, you cannot merge them.
You may want to put all of your .class files into a .jar.

If there is an inner class in your java file, then the compiler will create a class file with name of Java file along with $. You cannot combine these class files. But if you are copying these class files, make sure you are copying all the class files starting with $ symbol! else it will not work

Related

Is Empty .java file name working?

I started to learn Java couple days ago. And I have this burning question. Is empty .java file name a valid source file name?
.java
Yes, save your java file by .java then compile it by javac .java and run by java yourclassname
class X {
public static void main(String args[]) {
System.out.println("Hello World");
}
}
to compile - javac .java
to execute - java X
Yes it's working because java compiler doesn't consider it saves file name or not except our class having public specified we can save any name or empty but when ever trying to execute we must use our class name because, jvm creates byte code ourclassname.class so we using
java className
Yes Empty .java file name works, but class must not be public, it means that it must be default.
If class is public then following error occour:
D:\Testjavac>javac .java
.java:1: error: class Empty is public, should be declared in a file named Empty.
java
public class Empty
^
1 error
Yes You can have .java file withought nay name . you have to compile it by javac .java(it compile successfuly) and run it by java clasnname.(so you must provide a class name)
Yes, but don't do this often.
You can't create any classes in that file that are public or private, so any class that made use of any class defined here would have to be in the same package.
at anytime you can have only one public class in the file and if you use public class then that class name should be the file name.

Briefly, main function behavior - multiple classes in one source file

Given a Java source file named as TestMainFunction.java
1.) As most of us would know, a Java file name must be the class name which contains the main function
See the below simple code (in a single file named as mentioned above) which executes perfectly and prints ClassOne.
public class TestMainFunction {}
class ClassOne {
public static void main(String[] a) {
System.out.println("ClassOne");
}
}
2.) When there is more than one class with a main function in a single file, Eclipse prompts the user to choose the class to run.
See the below simple code (single file named as mentioned above) which executes perfectly and prints the class name as we have chosen from the Eclipse promt.
public class TestMainFunction {
public static void main(String[] a) {
System.out.println("TestMainFunction");
}
}
class ClassOne {
public static void main(String[] a) {
System.out.println("ClassOne");
}
}
class ClassTwo {
public static void main(String[] a) {
System.out.println("ClassTwo");
}
}
All the above will work as I mentioned. It will create separate .class file for every class. The same behavior applies even when using inner classes.
But what exactly is happening here? How does Eclipse know which class to launch when multiple classes are present in one source file? Can any one explain it? Explaining this would be greatly appreciated.
As most of you would know, a Java file name must be the class name
which contains the main function
This is incorrect, and I believe the source of your confusion. A Java source file is only allowed to have one public class, and it must have the same name (minus the extension) as it's containing file. A given Java source file though, may contain as many non-public class files as desired, with the only constraint being that their names are valid. Note that you can have a class with the same name as its containing source file (minus the extension) that is not public! Also note that it's generally considered bad practice to have multiple top-level (non-nested) classes in a single Java source file.
The second assumption you may have is that only one class in a source file is allowed to have a main function. This is simply untrue. You can add a main function to every single one of your class files - the only one that matters is the one you specify to the JVM when your application is being launched.
So given your scenario - a source file with multiple class files (one of them public), where each class has a main method, the following applies:
When you invoke the Run command in Eclipse on this file, Eclipse will detect that there is more than one eligible class to be run, and will prompt you to select one of those class. It will then build a 'Run Profile' for the selected class, that launches just that class. You can actually see the profile Eclipse builds via the Run->Debug Configurations menu.
When compiling this source file externally, it will generate multiple .class files (not just one). The classes had no relation to each other save being in the same source file, and you would explicitly select the .class you want to launch in the JVM.
i.e:
java TestMainFunction
java ClassOne
java ClassTwo

How to ignore java filename?

It gives an error "Could not find the main class: filename.java" How do I set the filename to be independent of the class names?
You can't ... In Java the file name has to match the name of the public class in the file
See Why are filenames in Java the same as the class name? for an explanation
Short Answer: You can't. One class per file is the java way. Accept that or find another language.
Longer Answer: You can but you probably don't want to.
If you have one public class and x number of non-public classes, you can put the all in the same file by nesting the non-public classes inside the public class. For example (in BlowFish.java):
public class BlowFish
{
class Hooty
{
}
class Sushi
{
}
}
you can't. a java class must be in a .java file with the same name.
maybe is into the manifesto file where you are defining this class as your main class
Sure you can. Just put version numbers in your classnames, as well.
Or keep the newest as the classname.java, with older versions getting version numbers.
Or drop the version numbers and use source code control.

What should be the name of a Java source that contains more than one class?

If there is more than one class in one Java source file then what will be the file name of the .java file?
there can only be one public top level class in a java file. The name of the public class must match that of the file name. Other than this, there can be as many non public (default/package access) classes as you like.
None of this is part of any java specification, it is just convention but a very convenient one. This 'convention' also includes such things as java and class files being found in directory structures matching the package name of the class. Check out the java tutorial on this.
Only one class can be public in the same file and the public class is the file name...

Defining classes in Java files

I have found one error in my Java program:
The public type abc class must be defined in its own class
How can I resolve this error? I am using Eclipse. I am new to Java programming.
Each source file must contain only one public class. A class named ClassName should be in a file named ClassName.java, and only that class should be defined there.
Exceptions to this are anonymous and inner classes, but understanding you are a beginner to Java, that is an advanced topic. For now, keep one class per file.
Answering your addition: it is OK to inherit classes and that's totally fine. This does not matter, each class should still have its own file.
Public top-level classes (i.e. public classes which aren't nested within other classes) have to be defined in a file which matches the classname. So the code for class "Foo" must live in "Foo.java".
From the language specification, section 7.6:
When packages are stored in a file system (ยง7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
The type is referred to by code in other compilation units of the package in which the type is declared.
The type is declared public (and therefore is potentially accessible from code in other packages).
This rule, which doesn't have to be followed by compilers, is pretty much universally adhered to.
Ok, maybe an example will help.
In file MySuperClass.java:
public class MySuperClass {
// whatever goes here
}
public class MySubClass1 extends MySuperClass {
// compile error: public class MySubClass1 should be in MySubClass1.java
}
class MySubClass2 extends MySuperClass {
// no problem (non-public class does not have to be in a file of the same name)
}
In file MySubClass3.java:
public class MySubClass3 extends MySuperClass {
// no problem (public class in file of the same name)
}
Does that make things clearer?
A public class with the name of "abc" must be in a file called abc.java
You can create a new class an a existing file if it's private, but you should not do this.
Create one file per class.
Eclipse does that for you, if you create a new class.
For programming Java, you have to understand the construct of classes, packages and files. Even if Eclipse helps you, you have to know it for yourself. So start reading Java books or tutorials!

Categories

Resources