Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Call method from other class  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling methods from another class is confusing to me.
I have this code in one class:


   


The code at line 17 is what I have problems with.



Where MainActivity is the other class. How can I call the method updateCountDownText() without making anything static?

 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:The code at line 17 is what I have problems with.Where MainActivity is the other class. How can I call the method updateCountDownText() without making anything static?

Because the method you're calling is already static. And (probably) public.

How do I know that? Because that's how Java works.

public static methods can be called from anywhere by supplying the name of the class that the method is defined in. So the code above says:
"Call the static method updateCountDownText() defined in class MainActivity"

HIH

Winston
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Daniel Andersson wrote:The code at line 17 is what I have problems with.Where MainActivity is the other class. How can I call the method updateCountDownText() without making anything static?

Because the method you're calling is already static. And (probably) public.

How do I know that? Because that's how Java works.

public static methods can be called from anywhere by supplying the name of the class that the method is defined in. So the code above says:
"Call the static method updateCountDownText() defined in class MainActivity"

HIH

Winston


Hi,
No the method im trying to call is not static. Here's what my IDE says: "Non-static method 'updateCountDownText()' cannot be referenced from a static context."
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Daniel Andersson wrote:The code at line 17 is what I have problems with.Where MainActivity is the other class. How can I call the method updateCountDownText() without making anything static?

Because the method you're calling is already static. And (probably) public.

How do I know that? Because that's how Java works.

public static methods can be called from anywhere by supplying the name of the class that the method is defined in. So the code above says:
"Call the static method updateCountDownText() defined in class MainActivity"

HIH

Winston


But like you say the method is public. I guess that's a given if I want to call it from another class? What else do I need to do so that I can call this method from another class? :/
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:No the method im trying to call is not static. Here's what my IDE says: "Non-static method 'updateCountDownText()' cannot be referenced from a static context."

Ah. Well that would have been useful to know at the start.

So presumably somewhere in your code (although not visible to us) you've created a variable with the type MainActivity.

assuming it's called 'activity', then the way to do the call would be:
activity.updateCountDownText();

Daniel Andersson wrote:But like you say the method is public. I guess that's a given if I want to call it from another class? What else do I need to do so that I can call this method from another class? :/


Nothing.

Winston
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Daniel Andersson wrote:No the method im trying to call is not static. Here's what my IDE says: "Non-static method 'updateCountDownText()' cannot be referenced from a static context."

Ah. Well that would have been useful to know at the start.

So presumably somewhere in your code (although not visible to us) you've created a variable with the type MainActivity.

assuming it's called 'activity', then the way to do the call would be:
activity.updateCountDownText();

Daniel Andersson wrote:But like you say the method is public. I guess that's a given if I want to call it from another class? What else do I need to do so that I can call this method from another class? :/


Nothing.

Winston


Well not really. MainActivity is the name of the class where the method I want to call is created.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the function I want to call: (this function is created in MainActivity.java and I want to call it from another .java file or class)

 
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:Calling methods from another class is confusing to me.
I have this code in one class:


   


The code at line 17 is what I have problems with.



Where MainActivity is the other class. How can I call the method updateCountDownText() without making anything static?



Is the method setMillis static?

I gues that to call the method updateCountDownText you should create an instance of the class MainActivity , new MainActivity().uptdateCountDownText;

 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:This is the function I want to call: (this function is created in MainActivity.java and I want to call it from another .java file or class)


Then, since it isn't static, you need a named MainActivity object to call it on.

Winston
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Daniel Andersson wrote:This is the function I want to call: (this function is created in MainActivity.java and I want to call it from another .java file or class)


Then, since it isn't static, you need a named MainActivity object to call it on.

Winston


Okay any idea how that would look? :P
new MainActivity().updateCountDownText(); did not work.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ansamana Sankarray wrote:

Is the method setMillis static?

I gues that to call the method updateCountDownText you should create an instance of the class MainActivity , new MainActivity().uptdateCountDownText;



Yes the method  setMillis  is static.
But also want to learn how to call non-static methods. :/
Thanks for your code but it did not work in my case.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:Okay any idea how that would look? :P

private MainActivity activity = new MainActivity( ... );

then do the call as I suggested above.

Winston
 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Andersson wrote:. . . Yes the method  setMillis  is static.

Why? That doesn't sound a good idea; it means the milliseconds are independent of any object.


But also want to learn how to call non-static methods. . . .

Except in those instances where you create an object from a literal, by boxing, or via a method call, you would say this sort of thing:-Line 5 should be inside a method; line 3 should be in a constructor or a method; line 1 should be in a method (local variable) or in the class (field). You can abbreviate that code by joining the lines together; I think this is the shortest version:-
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! Winston beat me to it by one minute.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Daniel Andersson wrote:Okay any idea how that would look? :P

private MainActivity activity = new MainActivity( ... );

then do the call as I suggested above.

Winston


Do you mean?

MainActivity activity = new MainActivity();
               activity.updateCountDownText();

App crashes when I code it like this. Seems weird to me also to create a whole new instance of MainActivity() when I just want to call a method within it.
Oh well I might have to read more about this topic Thanks for the suggestions (y)
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you are confused about.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

spencer audrey wrote:Calling methods from another class is confusing to me.


Since this was an android example i solved it with something called "intent". Might not be useful for you.
Yes agree it's confusing . :/
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!