• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little help with my program please  RSS feed

 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers.I simply want to call methods t1 and t2 to the derived class MultiLoad,but I get 2 Errors.I will post my code below.I need to understand why these Errors came.


The following are the errors:
F:\MyJava\MultiLoad.java:21: non-static method t1() cannot be referenced from a static context
t1();
^
F:\MyJava\MultiLoad.java:22: non-static method t2() cannot be referenced from a static context
t2();
^
2 errors

Tool completed with exit code 1

Respect,
Ammar Salem

[ January 25, 2008: Message edited by: Ammar Salem ]
[ January 26, 2008: Message edited by: Bear Bibeault ]
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi -

This works fine:

The problem is that "t1()" and "t2()" are members of a class (class "Load") and can't be called independent of an instance of that class.

You need to create an instance of the class before you can start using it's members.

Make sense?

I hope that helps .. PSM
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Paul, but here is what I thought, t1() and t2() are inherited in the sub class MultiLoad. do we need to create objects and call the methods. what is the use of inheritance then?
thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to differentiate between static methods (also called class methods) and object methods. Object methods (like t1 and t2) can be called only on an instance of the class they're part of (either Load or MultiLoad). Static methods (those having a "static" qualifier in front of them) can be called without an object reference. In a static context (like the main method), no object reference is implicitly available, so those methods can be called only on an explicitly instantiated object.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friend.
Well... I'm not an expert, but when I'm in main, I create a instance of that class and call the methos from that reference. Is that wrong??

Thanks.

BTW, the code worked.



See you around... I'd like someone could answer my question ^.
Thanks.
BTW, sorry my English.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andre Brito:

...but when I'm in main, I create a instance of that class and call the methos from that reference. Is that wrong??


No, that is perfectly correct

And that is what Ulf said. As you are in main() method and it is being static and direclty invoked by JVM, you do NOT have (and also NO need to) have any object references of the classes as such. That's the speciality of main method (being public and static).

If at all you need to invoke your t1() and t2() and they are NOT static (instance methods), you need an instance. So, you need to explicitly create one and use to invoke the methods.

If you are not clear about these, kindly read about Static Vs Instance method in a Java Tutorial here -> http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html.

Hope this helps
[ January 26, 2008: Message edited by: Raghavan Muthu ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

I have adjusted the subject of this post for you.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so an inherited class can call the parent classes public AND protected methods?

Didn't know that. Makes sense I guess...


so if he would've made the methods t1() and t2() static he could've just called the functions like so:



right?


Justin Fox
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Fox:
so an inherited class can call the parent classes public AND protected methods?

Didn't know that. Makes sense I guess...


so if he would've made the methods t1() and t2() static he could've just called the functions like so:



right?


Justin Fox


Hi Justin.
Well... I have an interface, right? But when I code like this:



I have compilation error. So... Can I have static methods in interfaces or in some class that implements an interface? I tried, but I could not do that (compilation errors).

But yes... If I have the code:



It shows me the messages that are in methods t1 and t2 at the superclass.

I guess I'm wrong... anything, please, correct me
And sorry my english.
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello My friends,
even declaring my methods as static did not solve my problem.

F:\MyJava\MultiLoad.java:12: t2() in Load cannot implement t2() in LoadInt; overriding method is static
public static void t2()
^
F:\MyJava\MultiLoad.java:8: t1() in Load cannot implement t1() in LoadInt; overriding method is static
public static void t1()
^
F:\MyJava\MultiLoad.java:17: t2() in Load cannot implement t2() in LoadInt; overriding method is static
class MultiLoad extends Load
^
3 errors

Tool completed with exit code 1
[ January 26, 2008: Message edited by: Ammar Salem ]
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ammar,

I guess that interfaces can't have static methods. Or classes that implements interface.

I guess you didn't saw my post. Read it and you'll understand a little bit.

See you.
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Andre Brito , thank you every one for sparing your valuable time with me.Anyway,I now know what was wrong.
Regards,
Ammar Salem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!