Results 1 to 2 of 2

Thread: need an example

  1. #1
    mandrake446 is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default need an example

    How do you use a object, returned by a method, to call a method of a different class?

    just a simple example will do.

    Thanks,
    Mandrake

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class ClassExperiment {
        static AnotherClass anotherClass = new AnotherClass();
    
        public static void main(String[] args) {
            AnotherClass ac = getReference();
            String s = ac.someMethod();
            System.out.println("s = " + s);
        }
    
        private static AnotherClass getReference() {
            return anotherClass;
        }
    }
    
    class AnotherClass {
        public String someMethod() {
            return "someMethod of " + this;
        }
    
        public String toString() {
            return getClass().getName();
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •