How exactly is the Java compiler fetching code from other files (using "new" and "extends") without direct reference to those files (by only naming the classes)? is the compiler essentially reading all the Java files in the directory looking for relevant classes to include?
Example:
File 1:
public class Person {
public String name;
public Person(String name) {
this.name = name;
}
}
File 2:
public class Student extends Person {
...
...
Person you = new Person("foo");
When you reference a class called Person the compiler searches for a file called Person.class.
This search is done in each directory specified in the CLASSPATH (both the environment variable and the classpath specified in the arguments of java).
Note that the compiler first needs to know to what package a class belongs, so you can have two classes with the same name in different packages and still it will find the correct one (the one of the package you imported or that you fully qualified).
So if your class is actually: com.myorg.Person it will search in this classpath for a file:
com/myorg/Person.class
Related
I am working on a project that is having a layered Architecture.
I have a interface ABC and in that interface I have a enum XYZ
For ex
public interface ABC {
public enum XYZ {
CONSTANT1("SOMETHING"),
CONSTANT2("SOMETHING3");
final String name;
private TYPE(String name) {
this.name=name;
}
public String getName() {
return this.name;
}
}
}
I am compiling this using ant and using this jar file in other layer. In that layer I am trying to access it like
String name=ABC.XYZ.CONSTANT1.getName();
I am getting symbol not found error during compile. I verified classpath is set properly.
I am using ant v 1.8 and java 1.6.
Why I am getting this error ?
First of all, don't nest the enum in an interface. It's perfectly fine that it has its own source file named after the enum.
Second, I assume you mean XYZ instead of TYPE in your private constructor?
Last, you should be able to use it in that way, no matter if compiled via ant or within eclipse or directly using javac. Probably you have not compiled everything - the way you did it, there should be ABC.class (the interface), ABC$XYZ.class (the inner enum) and the class file of your calling class.
I am new to java and i am writing this code in notepad which is giving me errors.In netbeans although package is already defined.How to do this in notepad?
package A;
class A {
private String name;
protected String company="oracle";
A(String name) {
this.name = name;
System.out.println(name);
}
}
public class B extends A {
// A public class constant
public final static String st = "Public access modifiers";
B(String name) {
super(name);
}
void getCompany()
{
System.out.println(company);
}
}
package B;//getting class interface or enum expected
public class Application {
public static void main(String[] args) {
System.out.println(st);
B b=new B("Java");
b.getCompany();
}
}
You can not put different packages into the same source file... You have to create the appropriate folder structure, and separate Java source files for the sources in each package...
Also, to be able to reference classes from other packages, you have to import them appropriately, and make sure they are actually on the classpath both for compiling and running too......
Recommended reading
Java packages Wiki page
Java 7 package specification
Java Language Specification: import
package B;//getting class interface or enum expected
remove this line
Package declaration should be the first line of the source file.
You can not write 2 or more different packages with in the same source
The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.
The PackageOrTypeName must be the canonical name (§6.7) of a package, a class type, an interface type, an enum type, or an annotation type.
That is what it is telling, and remove multiple declarations of package
And you should import the class B, When they both belongs to different packages.
import packagePath.B;
If a single-type-import declaration imports a type whose simple name is n, and the compilation unit also declares a top level type (§7.6) whose simple name is n, a compile-time error occurs.
Language Specification
Side note: Do not write multiple classes in a single java file. Later It's very hard to maintain the code.
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
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...
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!