5 Replies - 321 Views - Last Post: 03 November 2017 - 10:37 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 205
  • Joined: 03-December 08

What does this mean?

Posted 31 October 2017 - 10:26 AM

Hello and thanks for reading!

I was hoping someone could help me understand a class file. I adopted it and I am reverse-engineering so I know how it works. Within this section of the code:

public class Main { 

        private static final Logger log = LogManager.getLogger(Main.class);

	private static volatile JAXBContext jaxbContext;

	private enum Status {
		SUCCESS, ERROR
	}

	private Main() {
		initialize();
	}

	public static void main(String[] args) { 
             ...



I am hoping to understand what this line does:

private Main() {
		initialize();
	}



Thanks for any insight you may be able to provide!

Cheers!
Mark

Is This A Good Question/Topic? 0
  • +

Replies To: What does this mean?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,603
  • Joined: 20-September 08

Re: What does this mean?

Posted 31 October 2017 - 10:58 AM

Err .. it initializes something ;) Whilst also preventing an instance of Main being created
Was This Post Helpful? 0
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 205
  • Joined: 03-December 08

Re: What does this mean?

Posted 03 November 2017 - 07:31 AM

Thanks for the reply and help. Upon further investigation I found the initialize() method that is being called:

private synchronized void initialize() {

		if (jaxbContext == null) {
			try {
				jaxbContext = JAXBContext.newInstance(TicketType.class);
			} catch (JAXBException e) {
				throw new RuntimeException(e);
			}
		}

	}



I'm still sketchy on what the private Main() part is for? I have seen it in a couple of other places throughout the class as well.

try {
			new Main().run(properties);
		} catch (IOException ioe) {
			log.error("Error: failed Main().run", ioe);
		}





Whenever I search for private Main() in google it gives me information about public static void main(String[] args). I know what that is, but not private Main(). I think it refers to the class because in Java, the convention is that class names begin with a capital letter and method names do not. Thanks again!

Cheers,
Mark

This post has been edited by midasxl: 03 November 2017 - 07:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,603
  • Joined: 20-September 08

Re: What does this mean?

Posted 03 November 2017 - 07:49 AM

Main() is the invocation of the private constructor of class Main
Was This Post Helpful? 0
  • +
  • -

#5 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 205
  • Joined: 03-December 08

Re: What does this mean?

Posted 03 November 2017 - 09:58 AM

Ok, so I took your response and researched a bit more. Here's how I understand it:

Within the java main method:

public static void main(String[] args)



An instance of the Main class is created, and at the same time the run() method is called:

try {
	new Main().run(properties);
		} catch (IOException ioe) {
			log.error("Error: failed Main().run", ioe);
    }



This will invoke the Main constructor to create an instance of Main, which in turn runs initialize():

private Main() {
		initialize();
	}



The private part of the constructor means that it is the only thing allowed to create an instance of Main. It seems at the same time the Main instance is created it also calls the run() method. So why does an instance of Main have to be created in order to call the run() method?

Why not just this?:

try {
	run(properties);
		} catch (IOException ioe) {
			log.error("Error: failed run()", ioe);
    }



Thanks again for helping me understand this!

Cheers,
Mark
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3444
  • View blog
  • Posts: 15,603
  • Joined: 20-September 08

Re: What does this mean?

Posted 03 November 2017 - 10:37 AM

Quote

So why does an instance of Main have to be created in order to call the run() method?


I can't answer that with precision without seeing the run method. I'm guessing because it's not static
In any case, initialize needs an instance to begin with
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1