Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Different format of 'new' keyword here?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all , I was watching a java swing tutorial at this webpage
In the main method, the guy added
Where test is the main class.
I'm more used to seeing something like:
So what is this difference all about?
Regards
Russell
 
Rancher
Posts: 188
7
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what happens here, the guy puts all the logic in the constructor of the Test class and then creates a new object without assigning it to any variable as he won't use it anyway.
 
Marshal
Posts: 76474
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At least that is the theory. What they are (I hope) actually doing is using the object just once and whatever is in the constructor will start off the EDT (=Event Dispatch Thread), which keeps the program running.
I would worry a bit about any tutorials using Test as a class name, especially if they spell it test.
 
Saloon Keeper
Posts: 14325
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(I hope)


I hope not. At any rate, it means the constructor is used for much more than it was intended, and even worse, it's very likely that the pack() or setVisible() methods are being called from the constructor. These methods kick off the EDT, which you must never do from a constructor.

Russell, your intuition is correct. While it's certainly possible to just run a constructor and have an application do what you want from it, it's extremely bad form.

For Swing GUIs, use the constructor to initialize and lay out components, but don't use it to display anything. Instead, provide a separate show() or display() method that resizes the window and optionally sets its location and then makes it visible. A typical main method should look like this:
 
Russell Sher
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the replies.
Regards
Russell
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's even worse - that's the code from the video:


Btw, do you have any references why someone "must not do" call methods from a constructor wich starts up the EDT?
 
Stephan van Hulst
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The EDT is just like any other thread, and you must not start threads from constructors. The reason for this is that the purpose of the constructor is to bring the object in a consistent state before you do anything else with it, and when you start a thread from a constructor you may leak a reference to the currently uninitialized object.

Take a look at this article by Bruce Eckel: https://www.bruceeckel.com/2017/01/13/constructors-are-not-thread-safe/

Technically it's possible to make a constructor thread safe by synchronizing the entire call on a static lock, but what would be the point? You still have to wait until the constructor call is finished, and you may as well just start the second thread after the constructor call. That's less error prone as well.

I believe there's also an article about this in Effective Java or Java Concurrency in Practice, but I can't find it right now.

A Google search for "Java constructor thread" yields loads of resources.

[edit]

Two relevant links:

http://www.javapractices.com/topic/TopicAction.do?Id=254
https://www.ibm.com/developerworks/java/library/j-jtp0618/index.html#4
 
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hansen wrote:It's even worse - that's the code from the video...



Yet another sin: calling a public method from a constructor.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me add another sin: Naming a method "gui".
 
Paul Clapham
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And another: calling setVisible() before the GUI setup is finished.
 
salvin francis
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:...before the GUI setup is finished.


Darn !! I missed that one. How about another one: naming an instance variable as "f"
 
Paul Clapham
Marshal
Posts: 27375
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Darn !! I missed that one. How about another one: naming an instance variable as "f"



It didn't have to be an instance variable. It's only used in one method, so it should be a local variable in that method.
 
Saloon Keeper
Posts: 26057
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hansen wrote:It's even worse - that's the code from the video:


Btw, do you have any references why someone "must not do" call methods from a constructor wich starts up the EDT?



That's valid, but this is the form I prefer:


A halfway compromise could code as "(new Test).doIt()", but I really don't recommend that. It's confusing to read and sometimes you end up expaning the main class code so you need to keep the instance of Test a little longer than originally planned.

There are a few cases where a discarding constructor are somewhat reasonable. Like this one:


However, that's still more likely to bite you in most cases than having a factory method that does the construction and add as separate steps.
 
Campbell Ritchie
Marshal
Posts: 76474
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . .. . . that's still more likely to bite you . . .

Rather than letting a this reference escape the constructor, that is makng a this reference escape. Definitely likely to bite you.

I think we ought to keep a database of YouTube videos by category: bad, really bad, utterly useless, etc.
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic