I have set up a class in a second file which is in the same package as the main but for some reason I am unable to call it without it giving a "cannot find symbol error" even though I'm sure everything is ok. This is a basic file I tried and it replicated exactly the same but im not sure if its my code or netbeans.
package filesystem;
public class FileSystem {
public static void main(String[] args) {
FileMethods(Hello);
}
}
and the other class
package filesystem;
public class FileMethods {
public void FileMethods(String myString){
System.out.println(myString);
}
}
This is the error i get next to the line
!
thanks for any help
Maybe something like this:
new FileMethods("Hello");
You cannot call a constructor directly without creating a new object.
Maybe you wanted to do
new FileMethods("Hello");
To print the string Hello.
Here you are trying to reference the variable Hello which doesn't exists.
Related
Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.
package abc ;
class Trying
{
Trying ()
{
System.out.println("hello");
}
}
public class trying {
public static void main(String[] args) {
new Trying () ;
}
}
In this when I change the name of class from Trying to some other name it works , but here it says :
Error: Could not find or load main class abc.trying
/Users/name/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Why is this happening ?
I didn't find such case in any of the questions already asked .
Java is case sensitive language, but there is no documentation that class name should be case sensitive or not.
In eclipse it will show you syntax error
Class file collision: A resource exists with a different case:
'/sample/bin/abc/Trying.class'.
OR
If not shows error it will create class file of only one class either Trying or trying.
1)
If class file of Trying class is generated then It will throw
Error: Main method not found in class abc.trying
Since there is no main method in class Trying, And at runtime it looking for main method to start.
2)
If class file of trying class is generated then It will throw
Exception in thread "main" java.lang.NoClassDefFoundError:
here at runtime it looking for class Trying since it called in main of class trying. It fails to load coz its not compiled.
So we can conclude java not allow two class with sameName even different case
more details of case sensitive of class name is here
class Trying
{
Trying ()
{
System.out.println("hello");
}
}
public class Try_Main {
public static void main(String[] args) {
new Trying () ;
}
}
Please use two different class names other than same name with different cases. On compilation, the compilation will success and compile will create two class files with same name but different cases. But, the OS only allows a single file and it simply overwrites first one( which created firstly on compilation, then second) by the second one. On running, you will get a run time error, because one of the classes is missing. So, please use different names...
Well, this class should be public and be sure of saving the file name as the class Name
You have specified Trying multiple times.
package abc;
public class Trying {
public static void main(String[] args) {
trying1();
}
public static void trying1() {
System.out.println("Good?!");
}
}
I compiled this code and then, TestInner$1.class emerged.
I know ~~~$1.class indicates that file has "anonymous class."
But I don't understand the reason why this class file made. I want to know the reason.
Here is the code.
public class TestInner {
private static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I tried another version removed "private" identifier, like the following.
public class TestInner {
static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I'd imagined that this code also would make TestInner$1.class file.
However it didn't create the file.
In addition, the following code, added Constructor, also didn't make TestInner$1.class.
public class TestInner {
private static class Inner {
Inner(){ }
}
public static void main(String[] args){
new Inner();
}
}
I have no idea, so can anyone help me?
EDIT:
I found the same question and it solved. Thank you for your helping.
Why is an anonymous inner class containing nothing generated from this code?
None of your examples have anonymous inner classes. None of them will produce a file named TestInner$1.class. All of them will produce a file named TestInner$Inner.class.
The following example shows an anonymous inner class and will produce TestInner$1.class:
public class TestInner {
public static void main(String[] args){
new Object() {
#Override public String toString () {
return "ninja";
}
};
}
}
I'm not sure where your TestInner$1.class came from but I'm guessing it's left over from previous experiments you were doing.
Update 1: I can confirm that without using Eclipse I get TestInner$1.class (in addition to TestInner$Inner.class -- 3 files are produced) for the first example but not for the last two, just like you are seeing. Will update when I find out why. When compiled via Eclipse, TestInner$1.class is never produced.
Update 2: OP found solution in Why is an anonymous inner class containing nothing generated from this code?.
Was just testing a simple code, and it appears my eclipse just got worse. This code is suppose to output 2. But when I run it, very weird error says 'Error: Main method not found in class jasc1, please define the main method as: public static void main(String[] args)' when my main method is clearly defined.
Does anyone know what this error is all about??
public class jasc1 {
int a = 2;
public void abc(){
System.out.print(a);
}
public static void main(String[] args){
new jasc1().abc();
}
}
This works fine for me, your file name must be wrong. It MUST be the same as the class name.
Additionally
Class names should (by convention) begin with an upper case letter so Jasc1
If you want to execute a class It should have a method public static void main(String [] args) (or similar meanings). But, for executing a class you have to run that class.
ex: In command line you call java jasc1 after compiling it with javac jasc1.java (ofcourse there are some options like -cs; see help)
The same way you can run one class in Eclipse or NetBeans IDEs by right-clicking on it in the project explorer and select Run or Run as
Of course, this jasc1 class cannot call another class that have a public static void main(String []args) method.
//Vector.java
package simple;
public class Vector{
public Vector(){
System.out.println("net.mindview.simple.Vector");
}
}
//List.java
package simple;
public class List{
public List() {
System.out.println("net.mindview.simple.List");
}
}
//LibTest.java
import simple.*;
public class LibTest{
public static void main(String[] args) {
Vector v = new Vector();
List l = new List();
}
}
When I try to set the classpath for Vector or List,
like
java classpath "C:\Learning Java\AccessControl" simple.Vector, I could Main method could not be found, please define main method. But in the book I'm using, neither file needs to have a main method.
If I try to run LibTest I get cannot access Vector and class file contains wrong class:Vector, errors.
From your posted code, nor Vector or List classes has the public static void main(String[] args) method in it, thus you getting the error.
Note that LibTest class has it, so it would be better to execute this class:
java classpath "C:\Learning Java\AccessControl" other.package.LibTest
To run package first you have to compile it from the directory
ex., C:\package-name\abc.java
package-name: as you mentioned simple.
After it you have to run "abc.java" file from the directory.
ex., C:____
Hope it will work