• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiate Object in main?  RSS feed

 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me again,

This does complie but I do not understand exactly why, there seems to be no object instantiation unless I am missing the point of 'new'. Is it because the main is a nested class within BeatBox?

If you need more code let me know - but I cannor find another line of code with BeatBox2

Thanks, James.

p.s I know I have left all of the import stuff off




 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have is a contraction of an instantiation and a method call into one line. If you wrote…you would have no problem understanding the code. the new Foo(...) bit instantiates the Foo object, and then the name f which has been given the type Foo is applied to it with the = operator.
Then, you call the baar(...) method on f.

But what if you never need that f reference again? We know that new Foo(...) represents a Foo object. So we can get rid of the second mention of f and its first occurrence, too.
Foo f = new Foo(...); f.baar(...);
   becomes
new Foo(...).baar(...);
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the keyword "new" dynamically allocates memory for an object at runtime.
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell - I think I'm with you on the concept, but to fully understand, if I built in.



would this work/compile?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have got it, but you need some pairs of round brackets () to get it to compile.
You need something in the Foo class, and something in the baar() method. I sometimes call such methods go(), as in new TreeNursery().go() in this post (last block of code, line 43).
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I've done some editing whilst you replied, but that is great. Thank you for your help.
 
BalaMurali dhar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class BeatBox {

JPanel mainPanel;
ArrayList<JCheckBox> checkboxList;
Sequencer sequencer;
Sequence sequence;
Track track;
JFrame theFrame;

public static void main(String [] args) {
BeatBox box =new BeatBox();
}

public void buildGUI() { //code here }
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still need one pair of (), but … you’re welcome
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One think that seems to have been missed in the answers is the BeatBox/BeatBox2 (and Foo/Foo2) conflict. The original code could only have compiled if the BeatBox2 class was defined somewhere (likely in other file - e.g. BeatBox2.java).
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, hence my original confusion. There is no BeatBox2 class.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that compiled there's definitely a BeatBox2 somewhere. If there's no source code I suppose it's possible there's a compiled version (.class file) hanging around that the compiler is using.
 
James Sands
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope, nothing - I've checked the book, nothing in there relating to a BeatBox2

UPDATE: Nope you're right - there is errata for this page. Which means I should have read this first! Thank you!

it's -

Sorry
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have nothing to be sorry for. You started an interesting discussion, and you couldn’t be expected to know that was a misprint.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!