I have a Java file TestThis.java like the following:
class A
{
public void foo()
{
System.out.println("Executing foo");
}
}
class B
{
public void bar()
{
System.out.println("Executing bar");
}
}
The above code file is compiling fine without any warnings/errors. Is there any way I could access any of class A or B without a top level class from any other external class?
If no then why does Java even permit compiling of such files without a top-level class?
As usual (for example, accessing from the Test.java):
public class Test {
public static void main(String... args) {
A a = new A();
a.foo();
B b = new B();
b.bar();
}
}
The rule here is that you could not have more than one public class in the source file. If you have one, the filename must match this public class name. Otherwise (your case), you can name your file as you wish. Other, non-public classes, will be package-visible and you can access them as usual.
Any other class in the same package can access A and B; in this case the null package is being used since no package statement is present for the source file.
Related
I want to call the main method of the same class with different string arguments from another class main method.
public class A {
public static void main(String[] args) {
String[] testArgs = {"Hi", "Helloworld" };
B.main(testArgs );
String[] testArgs1 = {"Hi", "Java" };
B.main(testArgs1 );
}
}
public class B {
public static void main(String[] args) {
System.out.println(args[0] + " " + args[1]);
}
}
Yes, you can call a main method exactly like you would call any other static method, from within the same class or from another class (as you already do).
1) If you put two classes, like A and B, in the same source file, exacly one of must be declared public. The name of this public class must be the prefix of the name of the file. (So, if A is the public class, the name of the file must be A.java, if stored in a ordinary file system.)
2) If you put A and B in separate source files, they can both be public.
Regardless of how you choose to store the classes, you call the main methods the same way. To call B's main method from A, write just
B.main( ... )
Yes you can ,but you just need to remove "public" from class B because in java a file have only one public class.
I am developing a plugin for an RCP application.
Within the plugin.xml, I need to register certain classes at a given extension point.
One of these classes is an anonymous (?) class defined like this:
package de.me.mypackage;
import org.something.AnotherClass;
public class ClassOne {
...
public static AnotherClass<ClassOne> getThat() {
return new AnotherClass<ClassOne>() {
...
};
}
}
Is there any way to reference AnotherClass<ClassOne> within the plugin.xml?
I already tried something like de.me.mypackage.ClassOne$AnotherClass but that does not work. Do I have to declare that class within its own file to be able to reference it?
As far as I know, it would have a numeric index:
class Bla {
public static void main(String[] args) {
(new Runnable() {
public void run() {
System.out.println(getClass().getName()); // prints Bla$1
}
}).run();
}
}
After compiling, you get:
$ ls *.class
Bla$1.class Bla.class
That said, you can't rely on the numbering in case the source file is modified.
Can you instead define a static inner class, like:
public class ClassOne {
public static class MyClass extends AnotherClass<ClassOne> {
public MyClass(/* arguments you would pass in getThat()? */) {
...
}
...
}
public static AnotherClass<ClassOne> getThat() {
return new MyClass(...);
}
}
I need to say the obvious here - you should make it a named class if you want to refer to it. Whether you can access it otherwise is a technical curiosity (that I don't happen to know the answer to), not something you should actually do in production.
The dollar sign only comes into play in the class's binary name; in Java source, just use de.me.mypackage.ClassOne.AnotherClass.class.
public class foo{int a;}
public class foo2{public static void main(String[] a){System.out.println("love");}}
The error is:
C:\Users\PUSHPAM\Desktop\java>javac foo2.java foo2.java:1: error: class foo is public, should be declared in a file named foo.java public class foo{
You can only have one public class per Java source file. The name of that file must match the public class name, so if you have a public class called MyClass, it must be in a file called MyClass.java.
Suppose if you have multiple classes in single program then you have to save your class with the class which have main() method. And that class should be public type. Don't make all the classes as public in one file.
Example:
class Vehicle {
//something
}
class Audi {
//something
}
class Manager {
public static void main(String[] args)
{
// something
}
}
main() method is available in Class Manager. Because of this i have to save the file with Manager.java.
javac Manager.java ---- .class files will generate for all the classes
java Manager --- getting output
How does this work?
EDIT: Being static is not a good explanation, because I can use non-static methods and it will all work. Updated the code to reflect that.
I have this in a file called Foo.java:
// This is in the Foo.java file
class Test {
public void printSomething() {
System.out.println("In Foo.Test");
}
};
and this in a file called Caller.java:
// This goes in the Caller.java file
public class Caller {
public static void main(String[] args) {
Test test = new Test();
test.printSomething();
}
}
I can execute my Caller and it will print In Foo.Test. How can this not be a compilation problem? I don't even have a Foo class created. I don't even have to define Foo.Test in the Caller.
This is on Eclipse Luna, Java8.
Java is weird like that. You could have a file without any lines of code and it would still compile. Go on try it.
Now, I think you are confusing Test with Foo.Test (I understand, it's Friday).
Intrinsically what you defined is this:
public class Foo {} // this is by default, but don't try to use it because you didn't define the scope
class Test {}
And your perplexity is "OMG, Test is the impure offspring of a non-existing class!!!", because you were expecting something like
public class Foo {
class Test {}
}
This has nothing to do with a method being static. It is about quirkiness in the javac.
Happy Friday everyone! Time for happy hour.
The main-Method of the Foo.java is declared static
calling static methods works without creating a object of the class.
E.g you can create following Method in Foo.java
class Test {
public static void test(String test) {
System.out.println(test);
}
};
Now you can call Test.test("No object will be created"); and there will be NO instance of Test
A java file could contain single public class, but it could have as much non-public classes (package-local in your case) as you wanted.
Foo.Test is for inner classes. The one you declared is top level type.
I took the following code from the K&B book "SCJP Sun Certified Programmer for Java 6 Study Guide":
class A { // 1
void m() {
System.out.println("outer");
}
}
public class TestInners {
public static void main(String[] args) {
new TestInners().go();
}
void go() {
new A().m();
class A { // 2
void m() {
System.out.println("inner");
}
}
}
class A { // 3
void m() {
System.out.println("middle");
}
}
}
As stated in the book, this code prints "middle". I infer that the class declaration marked as "3" is shadowing the one marked as "1", which is external to TestInners class.
If the classes were in different packages, I could resolve the ambiguity by qualifying one of them with the package name. But in this case the classes are not only in the same package but in the same file. How can I get an instance of the external class?
I saw the same question here but the accepted answer implies to modify the code adding an enclosing class to the whole thing. My question is how to get the instance using any type of qualifier or reference, if it's even possible.
Assuming your class is in package com.test, all you need to do is use
new com.test.A().m();
using the fully qualified name of the class.
If your classes are in the default package, ie. no package declaration, then you are out of luck and can't access the outer A.
In C++, you can explicitly address global scope by prefixing your symbol with ::, however, Java does not have such a thing.
So if you really want to get the outer A, you have to bite the bullet and do some other sort of enclosure, by for example wrapping it in another class or package.
EDIT: Here is another reason why.
object of innner-A can't be created before defining it.so use new A().m(); after define innner-A inside go() to access inner class object.
void go() {
class A {
void m() {
System.out.println("inner");
}
}
new A().m();
}
to access outer-A you have to append package name,in default package it is impossible to access outer-A.