• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please teach me...

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new to Java programming and I want to know how to use one class on to another class. Please teach me. Thanks.
 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lawrence,

I interpret your question as, "how does one class use another class".

class A, has to initialise class B in order to use the operations B offers
(that is, it's public interface).

If you paste the above in a file called A.java and compile it (javac A.java)
and run it (java A) you'll see "Im class B!". A uses B's print class. It
cannot use B's myOwnMethod however, since it's private.. but B can use it
itself.

Hope the above helped.

regards,

Svend Rost
 
Lawrence Buensalida
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's that simple? Thanks. I'll apply and practice. Thank you very much
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lawrence Buensalida:
it's that simple?


Err no not really. The example given was the simplest form of calling a method from one class by another. Read up on packages and on the different forms of encapsulation in Java. What would of happened if the method had been static, for example? Here is a link to the

Java Tutorial. Also how do you need to modify your classpath when calling a method from a class within a Jar file?
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic