Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shortcuts like VB's "with" keyword

 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very new to Java, though have about 10 years with VB. What I would like to know is there a process of shortcut that would allow not repeating object / method repition? eg
public class TryFlowLayout {
static JFrame aWindow = new JFrame("Flow Layout Window by DOCO");

public static void main (String[] args) {
Toolkit theKit = aWindow.getToolkit();
Dimension wndSize = theKit.getScreenSize();

aWindow.setBounds(wndSize.width / 4, wndSize.height / 4,
600, wndSize.height / 2);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
Container content = aWindow.getContentPane();
content.setLayout(flow);
for (int i = 0; i <= 6; i++)
content.add(new JButton("Press " + i));
aWindow.setVisible(true);
}
}
Replace the repition of aWindow for instance:
{...
With aWindow
.setBounds(...);
.getContentPane(...);
.setVisible(...);
End With
...}
Sure would be handy if there were
TIA
doco
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such shortcut in Java.
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was afraid of that seing I could not find anything in docs.
Thanks
doco
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that a "with" statement would mostly be used where you had the code smell "Feature Envy" - a part of the code of an object seems to be more interested about another object than about itself. For your own designs, you should think hard about moving that piece of code into the other class - sadly, you can't do this for third party libraries in Java, of course.
http://c2.com/cgi/wiki?FeatureEnvy
 
Donald R. Cossitt
buckaroo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I suppose the biggest problem I am going to have is shaking the VB out of my brain. It was not OO but object based - subtle but important difference. What you stated is a perfect example I suppose.
Thanks
doco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic