4

My friend is new to programming. He said he had a problem in his code and shared his screen on Skype. I saw he had instantiated his Main class to use all the functions, etc. It looked like this:

public static void main(String[] args) {
    Main main = new Main();
    main.Example();
}

I've also noticed this in a couple tutorials online. The thing is, since you shouldn't really have more than one main, why instantiate it?

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Hutch
  • 63
  • 6

4 Answers4

3

You could argue that it's better that way.

You should probably not have a Main class to begin with but one with a proper object oriented purpose that encapsulates something.

The main method has to be unfortunately inside one of your classes. But that doesn't mean that the poor class that has to house the main method isn't allowed to be instantiated.

In pure OOP there is no place for static at all. Not even utility classes: http://www.yegor256.com/2014/05/05/oop-alternative-to-utility-classes.html

zapl
  • 63,179
  • 10
  • 123
  • 154
  • It is sorta obsolete. I'm working on a dinky little program now and Main is just sitting there invoking outside methods and wasting space :\ – Hutch Nov 21 '15 at 03:58
1

This code initialize the Main class. Not public static void main() method !!!

It is a common thing in Java as per my knowledge as you can't always use static classes or methods.

you cannot access non-static variables or methods inside static methods (like main method). Hence the way to do is to create an instance of the class itself and use non-static variables or methods.

Refer this question

Community
  • 1
  • 1
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
1

The difference between using static and non-static context can be found here

Assuming your asking why he would create a object of his main class the answer would be to access non-static methods from within static context. Without that main class object, he would only be able to access static methods and classes. I would recommend looking into Object Oriented Programming basics, which can be found here.

 Main main = new Main(); //creates object of class Main
 main.Example(); //calls method Example from object main

What your friend did was create a instance of the class and store it into memory. He is not instantiating the main method, but the class Main. This gives him the ability to alter information belonging to his main object of the class Main.

The purpose for doing this is to access non-static methods in the class Main.

Community
  • 1
  • 1
Ryan
  • 1,972
  • 2
  • 23
  • 36
  • I understand the difference between static and non-static. Just I don't like the idea of having to instantiate something if I'm only using it once. – Hutch Nov 21 '15 at 03:43
  • The reason for adding those two lines is so you can access non-static methods – Ryan Nov 21 '15 at 03:46
  • @Hutch it isn't necessarily being used once. Probably, the `main.Example()` call is going to do a bunch of other stuff. That other stuff may include accessing other instance variables or instance methods of the Main object. – Doug Richardson Nov 21 '15 at 03:47
  • @Hutch also this is just a example situation. most programs will call many more methods then just one. – Ryan Nov 21 '15 at 03:50
  • I know this. But this is the Main class, so I don't see much reason why to use non-static. There is only going to be one static main class, so might as well make everything static and just refer to "Main.example" outside of classes. It honestly just doesn't feel right making an instance of Main. – Hutch Nov 21 '15 at 03:51
  • @DougRichardson IIRC he was just creating a JFrame and making labels, etc. I'm pretty sure he was asking about ripping the function he had and putting that into a separate class on it's own. – Hutch Nov 21 '15 at 03:52
  • you don't want to do everything in static – Ryan Nov 21 '15 at 03:54
  • @Ryan I was assuming it was convention, but wanted to ask if there was something I was missing on. Honestly, it just feels weird doing. – Hutch Nov 21 '15 at 03:55
1

The static methods can be easily accessed with the following format. ClassName.methodname(). But instance method of the class can not be called without creating. If we have all the methods static, in that case there is no need of the the creation of the objects as we do it in utility classes.

Hiren
  • 51
  • 5
  • Exactly. I want to be able to access Main wherever and not have to create a new instance just for a simple property. People linked me to other people's questions in which the classes are totally ok to be instantiated. However, I feel the Main is an exception. – Hutch Nov 21 '15 at 04:19