I am very new to this and I am curious on why this isn't working.
I have this class
public class TEST extends JFrame {
public static GamePanel gamepanel;
public static InventoryPanel inventorypanel;
public static TEST mainWindow;
private TEST() {
.....
}
public static void main(String[] args) {
mainWindow = new TEST();
}
}
Then, I want to access this class from another class that is in a different directory. But, it will not work. It always will say cannot resolve symbol. What am I doing wrong?
I want to be able to access gamepanel, inventorypanel and mainwindow.
(How would I be able to import that package?)
You can import classes by adding
import fully.qualified.class.name;
or the whole package with
import package.name.*;
The import statement should be located between the package and the class declaration.
IntelliJ should be able to import it automatically when aelecting the class and pressing Alt+Enter.
Related
//interface1.java
package package1;
public interface interface1 {
static final int a =10;
}
//StaticImportTest.java
import static package1.*; //import package1.*; works
class StaticImportTest {
public static void main(String args[]) {
System.out.println(a); //System.out.println(interface1.a) works
}
}
when i am replacing the word "import static" with only "import" and using System.out.println(interface1.a) it works, but not sure why its not working in its current form.
For your static import to work the way you intended it would have to be
import static package1.interface1.* or import static package1.interface1.a
A static import imports public static members of a class either all with * or a specific one like for example a.
A import on the other hand imports a package or specific classes from a package.
Your import static package1.* would try to import all members from the class package1 in the root package.
Making it a normal import and accessing a via interface1.a works because the import imports all classes from the package1 including interface1, therefor you can access a via the interface1 class.
I have two package:
java1 package with main class.
java2 package with end class.
I want to access function from end class to main class
java1 package - main class source code:
package java1;
import java2.end;
public class main extends javax.swing.JFrame {
public main() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
end.jTPanel();
}
}
java1 package - main class source code:
package java2;
public class end extends javax.swing.JPanel {
public end() {
initComponents();
}
public static void jTPanel(){
jTabbedPane1.setSelectedIndex(1);//always error, jComponent in jPanel
}
}
can you help me?
To access the method jTPanel from main you need an instance of end in main.
In the main class before your constructor, create a new instance:
public class Main extends javax.swing.JFrame {
End myend = new End();
Then you can access the jTPanel method by calling:
myend.jTPanel();
Side note: Java convention is to use capitalised class names. e.g. Main and End as apposed to main and end.
Make an instance of end class. Then using that instance call the methods of end class.
end x=new end();
x.jButton1ActionPerformed(event);
I have a public class Helper in another file that has a few public static variables declared. I'm wondering if it's possible to shorten the variable name so that I don't have to consistently use Helper.<variable_name> everywhere. It would be ideal if I could just use the variable name without the Helper prefix.
Use
import static a.b.c.Helper.<variable_name>;
where a.b.c is the package which contains the Helper class.
You can use a static import:
// Class1.java
package test;
import static test.Class2.static_var;
public Class1 {
public static void main(String[] args) {
System.out.println(static_var); // not Class2.static_var
}
}
// Class2.java
package test;
public class Class2 {
public static String static_var = "Hello";
}
Output from java test.Class1:
Hello
Wildcard imports also work:
import static test.Class2.*;
Note that you must specify the full package name in the static import declaration. It is not sufficient to import test.Class2; and then import Class2.*;.
I'm trying to learn java and seem to be missing something obvious.
In subdirectory lab I have the file Play.java
package lab;
import java.io.*;
public class Play {
public static void playprint(Object obj) {
System.out.println(obj);
}
}
My CLASSPATH starts with '.'
In the parent directory I have a program
public class test {
public static void main(String[] args) {
lab.Play.playprint("hello world");
}
}
This runs fine. If I change the program to
import lab.Play.*;
public class test {
public static void main(String[] args) {
playprint("hello world");
}
}
It fails with an error that it can't find symbol method playprint
What am I missing?
To import a method you have to use import static. Without you are trying to import all classes (and interfaces) within your class "Play" only.
import static lab.Play.*;
See the documentation on static imports for details.
If you want to skip using static methods, you can create an object of Play class inside the class test and then call playprint.
Can same class exist in multiple packages?
In other words, can I have Foo.java class in com.test.package1 and com.test.package2?
Update
Now I copied class from package 1 and placed in to package 2 and now I am creating an instance of that class, I want this instance to point to class present in package 1 but currently it points to package1 path, how can i modify it?
Oh so I cannot do something like:
Foo = new Foo() // pointing to Foo class in package 1
Foo = new Foo() // pointing to Foo class in package 2
Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.
Example: Suppose you have
pkg1/SomeClass.java
package pkg1;
public class SomeClass {
}
pkg2/SomeClass.java
package pkg2;
public class SomeClass {
}
and Main.java
import pkg1.SomeClass; // This will...
import pkg2.SomeClass; // ...fail
public class Main {
public static void main(String args[]) {
new SomeClass();
}
}
If you try to compile, you'll get:
$ javac Main.java
Main.java:2: pkg1.SomeClass is already defined in a single-type import
import pkg2.SomeClass;
^
1 error
This however does compile:
import pkg1.SomeClass;
public class Main {
public static void main(String args[]) {
new SomeClass();
new pkg2.SomeClass(); // <-- not imported.
}
}
Sure can but you'll need to distinguish which one you want when calling them in other packages if both are included within a source file.
Response to Comment:
com.test.package1.Foo myFoo = new com.test.package1.Foo();
com.test.package2.Foo myOtherFoo = new com.test.package2.Foo();
i was taken to this page by google when i had the error a type with the same simple name is already defined by the single-type-import. i fixed this error (AFTER A VERY LONG TIME) by realising the line import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; had snuck into the very top of my imports whilst i had the line import org.apache.commons.codec.binary.Base64; at the bottom of my imports.
So I was looking for a smarter solution than just using fully qualified names on one or both of the implemented classes.
If you create a private class, and extend your class, you are free to use the class, without writing the full package name each time.
Package 1
namespace namespace1.Logger
{
public class Log
{
public void Foo1(){}
}
}
Package 2
namespace namespace2.Logger
{
public class Log
{
public void Foo2(){}
}
}
My class implementation
//using namespace1.Logger;
//using namespace2.Logger;
namespace MyProject
{
public class MyClass
{
public MyClass()
{
LoggerA a = new LoggerA();
LoggerB b = new LoggerB();
a.Foo1();
b.Foo2();
}
private class LoggerA : namespace1.Logger.Log { }
private class LoggerB : namespace2.Logger.Log { }
}
}