-4

I am just a beginner and I am trying to learn other alternatives.

Is there another way to call a class from another class. For example, I have a class called Test, can you call it in another way from this one = Test example = new Test();

Toy99
  • 5
  • 5
  • 1
    You can "call" a class in different ways depending on it's type. For example, if the class is static (or has static functions), you can call its static members directly like `Test.MyFumction();` without `new`. Share your code if you need more help. – Racil Hilan Jan 06 '19 at 14:57
  • Possible duplicate of [Call Class Method From Another Class](https://stackoverflow.com/questions/3856413/call-class-method-from-another-class) – Isaac Jan 06 '19 at 15:02
  • There are no alternatives here. Each way is for a different purpose. The way you mentioned in your question is for a specific purpose that cannot be achieved in any other way. – Racil Hilan Jan 06 '19 at 16:40
  • Hi, maybe you mean instantiate a new class? – LeWoody Jan 06 '19 at 17:33

2 Answers2

1

You can have a Type Factory class which can create an instance for you dynamically. Read some articles about type factory. Its a very common way to dynamically create instances of a type.

Lets say you have a program that generates text files. As parameter you can ask for a specific template, the program should then browse through your classes of type text generator and create an instance matching your requested template.

eVolve
  • 1,340
  • 1
  • 11
  • 30
M.Hazara
  • 137
  • 9
0

If you need to create an instance of this class i.e this is not a static class then

Test example = new Test();

is the correct way to call this class.

If this was a static class e.g public static Test {... then you could call this class without creating a new instance of it e.g Test.SomeMethod Example Link

eVolve
  • 1,340
  • 1
  • 11
  • 30