File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How To access method from different class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How To access method from different class" Watch "How To access method from different class" New topic
Author

How To access method from different class

Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24
Hello Everybody,
I Have 2 Different . java files in same package beerselection i.e BeerExpert.java & SelectBeer.java
Both Classes Have public access,
BeerExpert.java contains public method called getBrands();
I Am Calling Above Method In SelectBeer.java As
BeerExpert be = null; & then
be = new BeerExpert();
It Says Could Not Find Symbol....
I Have Check Spellings & There Are no mistakes
I Am Not Getting What Is Wrong
Thank You
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 446

Rahul, welcome to Ranch family.

Best is to use some IDE. I created a poll for best IDE(s) on my blog the other day.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12926
    
    3

Rahul, welcome to the Ranch.

If you're a beginner with Java, I disagree about starting to use an IDE right away. It's better to learn the basics first, and concentrate on learning Java, instead of trying to learn Java and the IDE at the same time. It's better to start with a simple text editor. Notepad++ is a good and free text editor that has syntax highlighting for Java. When you're comfortable with the basics of Java, and know how to compile your source files on the command line, then it's time to have a look at IDEs.

Can you show us what your code looks like?

Note that in Java, your source file must have the same name as the public class or interface that you define in your source file. So if your public class is called BeerExpert, it must be in a file called BeerExpert.java. Java is case sensitive, so BeerExpert is not the same as beerExpert or Beerexpert.

How is your classpath set? Do you have a CLASSPATH environment variable set? If yes, then remove it - it's best to leave the CLASSPATH environment variable not set.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24




I Have Set ClassPath & is'nt that useful ,So that We can run java programmes from anywhere ??
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 446

I would still say to use IDE along with Notepad.
Using IDE is not a big task and you will only learn it while trying to overcome those compilation/running issues.
Otherwise you will have to again come to the issues you faced while using compilation and the fact is that once you start using IDE's, you will forget how you compiled your code from command line and will always need to refer the docs.

Anyway, compiling your code from command line does give me "Symbol not found errors" and all of those are related to Servlet.jar being not present in the classpath.

Can you recheck that servlet.jar/j2ee.jar is present in your classpath or you can post the exact symbol which was not found at your end...

One more thing, use the code tags when you post, it gives the feeling of IDE....
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12926
    
    3

Please UseCodeTags when you post source code. I have added them to your post above for you.

You didn't say earlier that you were creating a servlet. What application server are you using to run that servlet (Apache Tomcat? Glassfish? Something else?).

What commands are you entering to compile and run this, and what is the exact error message that you get (copy and paste the error message)?

Did you combine both classes, and do you have BeerExpert.class and SelectBeer.class in the same directory? Is the structure of your web application correct?
Rahul B. Shah
Greenhorn

Joined: Jan 17, 2011
Posts: 24
Hello,
When I Tried In Eclipse It Worked
Thank - You For Help
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 446

Then you were trying to compile in wrong way.

Good stick to IDE. You may need commands for writing ANT buildfile in future but rarely need to compile from command line.

Eclipse is the most voted IDE in the poll at my blog.

All the best.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Sunny Bhandari wrote:I would still say to use IDE along with Notepad. . . .
I would say that using Notepad along with an IDE makes your advice even worse than it was at first.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 446

Campbell Ritchie wrote:
Sunny Bhandari wrote:I would still say to use IDE along with Notepad. . . .
I would say that using Notepad along with an IDE makes your advice even worse than it was at first.


Well that was considering the advice given by Jesper de Jong in one post above my post..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Sunny Bhandari wrote: . . . Well that was considering the advice given by Jesper de Jong in one post above my post..
No, Jesper said to use Notepad++. Something totally different. And I still agree with him, it is best to avoid IDEs at the beginning stage.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 446

Campbell Ritchie wrote:
Sunny Bhandari wrote: . . . Well that was considering the advice given by Jesper de Jong in one post above my post..
No, Jesper said to use Notepad++. Something totally different. And I still agree with him, it is best to avoid IDEs at the beginning stage.


But Rahul finally resolved it using IDE. The debate continues....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Sunny Bhandari wrote:. . . The debate continues....
No, it doesn't.

I think we should shut up about IDEs, in this discussion at least.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To access method from different class
 
Similar Threads
Doubt in JSP Custom tags (Simple Tag) in Dynamic Drop Down
Unable to compile servlet with model class reference
Need a few JSP tips
servlet compiling errors
Why this EL code part is not working