• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please teach me...

 
Lawrence Buensalida
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Svend Rost
Ranch Hand
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's that simple? Thanks. I'll apply and practice. Thank you very much
 
Nigel Browne
Ranch Hand
Pie
Posts: 682
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic