Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Calling a method from another class

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you call a method from another class?
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create an instance of the class in which the method belongs to & call it.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean, in class A you have an object of class B, and you want to invoke method m on it? That would be "B.m()". Of course method m needs to be visible to class A, which means it can't be declared to be private (and -depending on your package structure- might need to be protected, package-visible or even public).
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So...using an example from class....


And then...

Is this what I need to do? Sort of follow this formula?

This is a lab sample we did in class, not my actual assignment.

What part of this is 'creating an instance of the class in which the method belongs to & call it.'
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code you are creating an instance of a cup class in some other class ( CupBoard ) and you are invoking methods in cup from CupBoard class like the code below.

Is that make sense. Is it clear ?
[ July 28, 2005: Message edited by: Srinivasa Raghavan ]
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, makes sense. I will work my actual homework following this as an example.

Thanks!
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...maybe I don't get it then...or I am doing something wrong...

Here is my actual assignment now...
This is what I am trying to do...I have created a clock disply that calculates the time as 24hr but displays as a 12 hr clock with AM and PM. NOW I need to add the day of the week to the mix.

Here is what I tried....


The DayDisplay class is as follows...
public class DayDisplay

The error I am getting is 'can't find symbol - day1'

Am I even close?
[ July 28, 2005: Message edited by: Lisa Beglaw ]
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at it again, I know what doesn't make sense...but I'm not sure how to make it make sense. I am tempted to just build the incrementDay code into the timeTick method. But I don't think that is what I am supposed to do.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Big nasty if/else statements like that can be replaced quite easily with a strategy[GoF]. Mind the verbosity.


[ July 28, 2005: Message edited by: Tony Morris ]
 
Lisa Beglaw
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tony -- I will print that off for future ref but I think that is a little ahead of where I am in class right now.

I think I am going to scrapy the whole DayDisplay thing and start from scratch. Sometimes that is just the best way to go when I get all tied up like this.

Thank you all.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic