• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help on main method!!  RSS feed

 
Hema lata
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Need help to understand how main method works. I know it's very very basic java. But i am lost when it comes to calling diffefent classes ot methods in main method.

My requirement is:
From main method, i want to call methods defined in xxx.class.
But i am getting compilation error when i compile main method.
Can any one help me to understand do's and dono't of main method.


my code:

ex: public class start{

public static void main(String[] args){

A a = new A();---> can this be done?
a.method1();--- can this be done?

}

}

public class A(String....){

public String method1(){

}
return result

}

what is the correct way to call methods from other classes, in main method

thanks in advance
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem lies in this code


public class A(String....){

public String method1(){

}
return result

}


Use instead


You can call methods from other classes in main method. However if the class A is in same file as in class "start" then you can't declare class A as public because in a single file you can declare only one class as public. If class A is in another java file then you need to import class A before using it. Like this

import somePackage.A;

Hope it helps
 
Hema lata
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ali. I still have some doubts. it would be great if you could help..!!

i have 2 java files
Start.java
a.java

Even if i define classes as below, i get compilation error. Some times i get error as non-static methods can't be accessed from static..

What should be the access modifiers for the methods in class A.



import pkg.A

public class start{
public static void main(String[] args){
A a = new A();--->here i get error as "A" is unidentified
a.method1();--- can this be done?
}
}

class A
{ public String method1()
{ return "someString"; }
}
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hema,


class A cannot be found, because you called the file a.java with a lowercase a. Save it as A.java


With the default access modifier (that is: no modifier written at all) all classes and fields (variables and methods) are visible in the entire package.

Ali Gohar posted November 02, 2006 12:01 AM
If class A is in another java file then you need to import class A before using it.



This is wrong.

Imports are never ever required!

When a class is in a different package (not file, as Ali wrote), you have to adress it with package and class identifier like for example


importing only saves typing action. When you have an import statement like
import java.awt.Point; or



And: if a class or field is not visible from the package where you want to adress it, you also cannot import it.


Yours,
Bu.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!