Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Different format of 'new' keyword here?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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: 133
7
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 11189
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the replies.
Regards
Russell
 
Ranch Hand
Posts: 118
5
  • Mark post as helpful
  • send pies
  • 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: 11189
244
  • Mark post as helpful
  • send pies
  • 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: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me add another sin: Naming a method "gui".
 
Paul Clapham
Marshal
Posts: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another: calling setVisible() before the GUI setup is finished.
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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.
 
Why is the word "abbreviation" so long? And this ad is so short?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!