0

I am a beginner in this java community so sorry if this question is so naive. I was wondering how to call a method form another class like how a println method of class System is called by using an object out.

i.e. how to use like

Class.object.method();
like in
System.out.println();

  • Maybe helpful: https://stackoverflow.com/questions/13259058/in-java-how-does-system-out-refer-to-printstream-class – Thilo Oct 01 '17 at 04:03
  • In your example, `System.out` is an object of type `PrintStream`. The method `println()` is a method of class `PrintStream`, not of class `System`. But it's not clear what question or problem you have. – Ted Hopp Oct 01 '17 at 04:03

1 Answers1

1

Thats where you use static stuff like

class Class {
  public static AnotherClass object = new AnotherClass();
}

class AnotherClass {
  public void method() { 
    //... 
  }
}
JSelser
  • 3,510
  • 1
  • 19
  • 40
  • This is closely related to the Singleton pattern. – Thilo Oct 01 '17 at 04:09
  • Not really, you wouldnt be disallowing neither the creation of new instances nor ensuring `Class.object` is unique – JSelser Oct 01 '17 at 04:10
  • Yes, it is not a singleton. But it is still a related pattern: You provide access to a well-known instance for the whole of the rest of the system to call methods upon (without having to worry about instantiating the instance themselves) – Thilo Oct 01 '17 at 04:11
  • 1
    Add `final` modifier to `object` to make it more related to the Singleton pattern. – IllegalArgumentException Oct 01 '17 at 04:20