Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hurry!!

 
steven Shand
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked whether "." is included in your CLASSPATH variable?

Nick
 
steven Shand
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm doing this thro Textpad, compiling it then run it not thro command prompt.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post the error logs so that your problem can be understood a little more clearly?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic