Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Hurry!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hurry!!" Watch "Hurry!!" New topic
Author

Hurry!!

steven Shand
Greenhorn

Joined: Nov 16, 2004
Posts: 9
Basically got a Coursework to hand in today and have major problem.

Im trying to call a method in another class from the main class.

ie.


an the method is being call in


Have try import statements, import getDay; and try placing in another directory package packagename.getDay;, but have had no luck.

Both files are in the same directory.

Cheers.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Have you checked whether "." is included in your CLASSPATH variable?

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
steven Shand
Greenhorn

Joined: Nov 16, 2004
Posts: 9
i'm doing this thro Textpad, compiling it then run it not thro command prompt.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
i'm doing this thro Textpad, compiling it then run it not thro command prompt.

But still, you need to set the CLASSPATH somewhere (no matter the TextPad or any editors) when you compile and execute your code, otherwise, it wont work!

Nick
steven Shand
Greenhorn

Joined: Nov 16, 2004
Posts: 9
Sorry mate, got a wee bit stressed there!

Yeah the classpath is set to the current directory that im running the files from.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Your problem should be, as "getDay" is the name of the class, so do the method name is the same name, you should new the class object and then call its *getDay* method.

I am not sure whether you can name a method as same as the class name, in case you can, you should call it by:


Not just by invoking it, as they come from different objects.

But still, I guess you better either rename your class, or method to avoid ambigirity.

Nick
[ November 16, 2004: Message edited by: Nicholas Cheung ]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
You should have problem in compile time.

Please check follow me ::

Example :

you have java source code in c:\src directory . and you will keep classes file in c:\classes directory.

So, you MUST set CLASSPATH environment like below :



Compile source code
c:\source > javac -d %p% *.java


This will help you, if you have error, please let me know.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Can you please post the error logs so that your problem can be understood a little more clearly?
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I think the origin of the problem is:

But getDay() does not belong to that class. This shouldnt be any classpath issues, or execution issues at all.

Nick
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Normal practice is to name classes as Nouns and methods as Verbs. Also only constructors should share the name of the class. For example


Follow Nick's advice and rename your getDay class and then initialize an instance of that class to access it's methods.
[ November 17, 2004: Message edited by: Nigel Browne ]
Nikhil Vasaikar
Ranch Hand

Joined: Aug 18, 2004
Posts: 56

Originally posted by Nicholas Cheung
Your problem should be, as "getDay" is the name of the class, so do the method name is the same name, you should new the class object and then call its *getDay* method.

I am not sure whether you can name a method as same as the class name, in case you can, you should call it by:



This is exactly what is required to be done. You cannot call a method without the classname or object reference, unless it is present in the same class or inherited.

Nikhil
steven Shand
Greenhorn

Joined: Nov 16, 2004
Posts: 9
Cheers for all the info, ive handed it in now. Just amended it so it would run with the method in the class.

but I will go away and try what u've mentioned and get back to us's

Thanks for helping tho!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hurry!!