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.
Related
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.
I created a static method which returns a static int in class NewTriangle. But when I try to call it from the main it won't print. It asks to create a method with the same name inside the main.
public class NewTriangle{
public static in numberOfTriangles;
public static int getnumberOfTriangles(){
return numberOfTriangles;
}
}
The code works up until this point. But When I call getnumberOfTriangles() from the main I get an error.
public static void main(String args[]){
System.out.println(getnumberOfTriangles());
}
If your main method is in a different class, then you need to specify the classname while calling the static method, i.e., NewTriangle.getnumberOfTriangles()
Assuming the typos in your code are copy and paste errors, you need to either
Use the class name before the method name, you may need to add the class to your import statements (you need to if it is in a different package)
System.out.println(NewTriangle.getnumberOfTriangles());
Add a static import of the getnumberOfTriangles method to your main class
import static NewTriangle.getnumberOfTriangles;
However, note the caution given in the link:
So when should you use static import? Very sparingly!
You have to write the name of the class before :
NewTriangle.getnumberOfTriangles()
You can do something like this:
Example using :
I have created a package name stackoverflow
Two class callerClass and NewTriangle in package stackoverflow.
Make an import of class NewTriangle in callerClass.
using the Static function by NewTriangle.getnumberOfTriangles()
NewTriangle.getnumberOfTriangles() // className.StaticfunctionName()
Working Code:
Class 1 :
package stackoverflow;
public class NewTriangle {
public static int numberOfTriangles;
public static int getnumberOfTriangles() {
return numberOfTriangles;
}
}
Class 2 :
package stackoverflow;
import stackoverflow.NewTriangle;
public class callerClass {
public static void main(String args[]) {
System.out.println(NewTriangle.getnumberOfTriangles());
}
}
Java static method :
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.
I hope I was helpful :)
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 have written a simple java program mentioned in below. Unfortunately a compile error occurs.
class String {
public static void main(String[] args) {
System.out.println("stre");
}
}
The following comes out during compilation at command prompt:
c:\Java>java String
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args)
It wasn't working for any of my programs, not even this simple one! Why is this?
EDIT:
Now I have:
import java.lang.*;
import java.lang.String.*;
class incogn {
public static void main(String[] args) {
System.out.println("stre");
}
}
And its not working. Why isn't that working?
It says the exact thing as before.
What you said with the Java.lang.String[] works, but why won't this? And why haven't I needed to put any of this on before?
It's probably because you are using the name String for your class, colliding with java.lang.String; in other words, you have written that your main method takes instances of your class as input, rather than the one that the main method needs to take, namely java.lang.String. Try either
Renaming your class
Changing your signature to public static void main(java.lang.String[] args) {
You should not call your class String, it will conflict with the java class String.
Or, you can change your code to:
class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
Don't create a java file named "String" as it is the java library class in java.lang.String
Make the class as public
So, write your source code as below:
public class StringDemo {
public static void main(String[] args) {
System.out.println("stre");
}
}
Compile it now. I believe it would work well.
1) The class should be public
2) Avoid to call you class String, as it has the duplicate name with java.lang.String (You still can, but not good). Otherwise, your main method is having wrong type of arguments.
3) You don't have to explicitly import from java.lang package.
public class String {
public static void main(java.lang.String[] args) {
System.out.println("stre");
}
}
OR
public class YourClass {
public static void main(String[] args) {
System.out.println("stre");
}
}
Try to use the second form.
Well, giving your class name String and then call main(String[] args) is ambiguous, if it means java.lang.String or your newly defined class, but with java scope rules, it will use your, unless otherwise declared.
Update after edit
There's no java.lang.String package, but it is a class. Therefore, you can't say
import java.lang.String.*;
By adding the ".*" the compiler understands that this refers to some package String.
One exception, is the static import, but this is not what you need here.
How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
I saved the first code in C:\Dan\Vik.java
and the second in C:\Kab.java
You don't import methods in Java, only types:
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
The exception is so-called "static imports", which let you import class (static) methods from other types.
In Java you can only import non-primitive types, or static methods/fields.
To import types use import full.package.name.of.TypeName;
//example
import java.util.List; //to import List interface
to import static methods/fields use
import static full.package.name.of.TypeName.staticMethod;
import static full.package.name.of.TypeName.staticField;
//example
import static java.lang.Math.max; //to import max method(s)
import static java.lang.Math.PI; //to import PI field
Take out the method name from in your import statement. e.g.
import Dan.Vik.disp;
becomes:
import Dan.Vik;
You should use
import Dan.Vik;
This makes the class visible and the its public methods available.
Here is the right way to do imports in Java.
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
You don't import methods in java. There is an advanced usage of static imports but basically you just import packages and classes.
If the function you are importing is a static function you can do a static import, but I don't think you are looking for static imports here.
In Java you can only import class Names, or static methods/fields.
To import class use
import full.package.name.of.SomeClass;
We can also import static methods/fields in Java and this is how to import
import static full.package.nameOfClass.staticMethod;
import static full.package.nameOfClass.staticField;
For the second class file, add "package Dan;" like the first one, so as to make sure they are in the same package; modify "import Dan.Vik.disp;" to be "import Dan.Vik;"