I have an outer class and a static nested class as
class Outer {
static int x = 20;
static class Nested {
static void show() {
System.out.println(x);
}
}
}
on compiling this program ,I've got two .class files named:
Outer.class and Outer$Nested.class .
Now my question is ,I am trying to use the Outer$Nested.class in my other class Test1 (using association).
class Test1 {
public static void main(String... args) {
Outer$Nested i = new Outer$Nested();
}
}
It gives me compile time error : can't find symbol Outer$Nested.
And I know we can use static nested class directly as Outer.Nested, but I am just trying this.
But when I am trying to use this class via Inheritance then it is compiling and running fine.
class Temp2 extends Outer$Inner {
public static void main(String... args) {
show();
}
}
Now compiling and running fine , output : 20
So my question is why it is happening that we aren't able to use Outer$Nested via association and event we can't declare variable , but can use via inheritance ?
I didn't know how the inheritance concepts works like your way, but want to create instance of the inner nested class use this lines of codes
public static void main(String[] args){
Outer.Nested obj= new Outer.Nested();
obj.show();
}
****EDIT** For everyone saying call it like
FunctionTestTest.numberCheck(userNumber);
I have tried that numerous times before posting on here it didnt work.
People downvote a question that they cant even answer, great...
On another project Im working on I couldn't call functions from another class. Been trying to fix it all day. Decided to throw up a a few lines of code & try call a function from another class just to make sure i didn't have an unnoticed syntax error in my main project.
can anyone see what the problem is here?
returning this error:
cannot find the symbol
symbol: class FunctionTestTest
location: class FunctionTest
...
public class FunctionTest{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int userNumber = 0;
System.out.println("Please enter a number between 1 - 10");
userNumber = input.nextInt();
FunctionTestTest ft = new FunctionTestTest();
FunctionTestTest.numberCheck(userNumber);
}
}
and..
public class FunctionTestTest{
public static void main(String[] args){
}
public static void numberCheck(int num){
if (num == 1){
System.out.println("function works");
}
}
}
The error is caused because you are probably using class from another package. In this case you have to import it first before using.
If you are using any IDE there should be a hotkey to fixing your problem.
Also...
You don't need to create an object instance to access static methods of certain class. Simply use:
FunctionTestTest.numberCheck(userNumber);
It is not recommended, but you can call a static method on object instance like:
new FunctionTestTest().numberCheck(userNumber);
here you are calling other class constructor and is not present there:
FunctionTestTest ft = new FunctionTestTest(userNumber);
also check if FunctionTestTest class present is some other package then import it
ft.(userNumber);
Your method invocation is incorrect.
Your probably wanted ft.numberCheck(userNumber) or FunctionTestTest.numberCheck(userNumber). The latter is preferred as you are incoking a static member.
If is it a static method, you just call it as:
ClassName.methodName();
There is no need to instantiate an object to use a static method like what you did.
Example:
public class MyClass{
public static void myMethod(){
//do whatever..
}
}
public class OtherClass{
public static void main(String[] args){
MyClass.myMethod(); //invoke a static method from another class
}
}
Please help me out in out to get rid of this error. Whereas i tried the same code in Command line it was working perfectly without any exceptions.
public class String {
public static void main(String[] args) {
StringBuilder nb = new StringBuilder("");
nb.append("<string1>");
nb.append("<string2>");
System.out.println(nb.toString());
}
}
Error: Main method not found in class String, please define the main method as:
public static void main(String[] args) or a JavaFX application class
must extend javafx.application.Application
Don't name your class String as String is also a "build-in" type of Java (java.lang.String).
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.
I was wondering what the effect of creating extra main methods would do to your code.
For example,
public class TestClass {
public static void main (String[] args){
TestClass foo = new TestClass();
}
}
After the program initially starts up, foo will be created and it would have another public main method inside it. Will that cause any errors?
It will cause no errors. Just because you initialize an object, doesn't mean the main method gets executed. Java will only initially call the main method of the class passed to it, like
>java TestClass
However, doing something like:
public class TestClass
{
public static void main (String[] args)
{
TestClass foo = new TestClass();
foo.main(args);
}
}
Or
public class TestClass
{
public TestClass()
{
//This gets executed when you create an instance of TestClass
main(null);
}
public static void main (String[] args)
{
TestClass foo = new TestClass();
}
}
That would cause a StackOverflowError, because you are explicitly calling TestClass's main method, which will then call the main method again, and again, and again, and....
When in doubt, just test it out :-)
The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all.
You could call the main method on instances of the object, but if you do that it's literally just another way of calling TestClass.main() (and it's frowned upon by many, including me, to call a static method on an instance of an object anyway.)
If you're referring to multiple main methods in the same program, then this isn't a problem either. The main class is simply specified and its main method is executed to start the program (in the case of a jar file this is the main-class attribute in the manifest file.)
It won't have an additional main-method, as main is static. So it's once per class.
If you have multiple main-methods in your project, you will specify which one to launch when starting your application.
This is perfectly fine. Having multiple main methods doesn't cause any problems. When you first start a Java program, execution begins in some function called main in a class specified by the user or by the .jar file. Once the program has started running, all the other functions called main are essentially ignored or treated like other functions.
After searching for a Java Class with multiple main() methods or in plain words, overloaded main() methods, I came up with an example of my own. Please have a look
public class MultipleMain{
public static void main(String args[]){
main(1);
main('c');
main("MyString");
}
public static void main(int i){
System.out.println("Inside Overloaded main()");
}
public static void main(char i){
System.out.println("Inside Overloaded main()");
}
public static void main(String str){
System.out.println("Inside Overloaded main()");
}
}
I tested this Java Code on JDK 1.7 and works like a charm !
You need "public static void main(String args[])" to start with and then you can call overloaded main methods inside this main and it should work for sure.
Any comments and suggestion are highly appreciated. I am just a novice Java Developer willing to develop my Java skills.
Thanks,
PK
No, you can have any number of main-methods in a project. Since you specify which one you want to use when you launch the program it doesn't cause any conflicts.
You can have only one main method in one class, But you can call one main method to the another explicitly
class Expmain
{
public static void main(String[] ar)
{
System.out.println("main 1");
}
}
class Expmain1
{
public static void main(String[] ar)
{
System.out.println("main 2");
Expmain.main(ar);
}
}
when you run your Java class it will always look for the signature public static void main(String args[]) in the class. So suppose if you invoking by command line argument, it will look for the method Signature in the class and will not invoke other until if u explicitly inoke it by its class name.
class MainMethod1{
public static void main(String[] ags){
System.out.println("Hi main 1");
testig2 y = new testig2();
//in this case MainMethod1 is invoked/.......
// String[] a = new String[10];
// testig2.main(a);
}
}
class MainMethod2{
public static void main(String[] ags){
System.out.println("Hi main 2");
}
}
But when you try the same from eclipse it will ask for which class to compile. Means MainMethod1 or Mainmethod2. So if te class has the exact signature they can be used as individual entry point to start the application.
Coming to your question, If you remove the signature as u did above by changing the argument if main method. It will act as a normal method.
It is all about the execution engine of JVM. Remember, you write >java com.abc.MainClass on cmd prompt.
It explains everything. If main method is not found here it throws a run time Error:Main Method Not Found in class MainClass.
Now if main method is found here, it acts as the first point when Program Counters have to map and start executing the instructions. Referred classes are loaded then, referred methods may be called using the instances created inside. So, main is class specific though one class can have only one main method.
Please note, main method's signature never changes. You can have two overloaded main methods in same class, like
public static void main(String[] args) {}
public static void main() {} //overloaded in same class.
During Static binding, the original main is resolved and identified by execution engine.
Another interesting point to consider is a case where you have two different classes in of java file.
For example, you have Java file with two classes:
public class FirstClassMultiply {
public static void main (String args[]){
System.out.println("Using FirstClassMultiply");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiple is :" + mult.multiply(2, 4));
}
public static void main (int i){
System.out.println("Using FirstClassMultiply with integer argument");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiply is :" + mult.multiply(2, 5));
}
int multiply(int a, int b) {
return (a * b);
}
}
class SecondClass {
public static void main(String args[]) {
System.out.println("Using SecondClass");
FirstClassMultiply mult = new FirstClassMultiply();
System.out.println("Multiply is :" + mult.multiply(2, 3));
FirstClassMultiply.main(null);
FirstClassMultiply.main(1);
}
}
Compiling it with javac FirstClassMultiply.java will generate two .class files, first one is FirstClassMultiply.class and second one is SecondClass.class
And in order to run it you will need to do it for the generated .class files: java FirstClassMultiply and java SecondClass, not the original filename file.
Please note a couple of additional points:
You will be able to run SecondClass.class although it's class wasn't public in the original file!
FirstClassMultiply overloading of the main method
of is totally fine, but, the only entry point to your prog
will be the main method with String args[] argument.