Making a new array in a call to this

i need to make an array in a contructor in the call to this()

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1151 Views - Last Post: 22 January 2010 - 03:05 PM Rate Topic: -----

#1 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Making a new array in a call to this

Posted 21 January 2010 - 03:25 PM

Hopefully someone here can help me quickly, i think the code will say what i want to happen

Current Code Snippet:
	   public Panel03(int size, int sec)
	  {
		 this(size, sec, getC());
	  }
	   public Color[] getC()
	  {
		 Color[] tc = new Color[2];
		 tc[0] = Color.white;
		 tc[1] = Color.black;
		 return tc;
	  }
	   public Panel03(int size, int sec, Color[] c)
	  {


old code:
	   public Panel03(int size, int sec)
	  {
		 this(size, sec, new Color{Color.white, Color.black});
	  }
	   public Panel03(int size, int sec, Color[] c)
	  {


Is This A Good Question/Topic? 0
  • +

Replies To: Making a new array in a call to this

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:31 PM

Can you describe the errors you are getting as well as the context of your code?
Was This Post Helpful? 0
  • +
  • -

#3 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:32 PM

Sorry, forgot ^^;

Panel03.java:25: cannot reference this before supertype constructor has been called
		 this(size, sec, getC());
						 ^
1 error

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:35 PM

You cannot call an instance method while your object is not created yet and it is not created yet because you invoke another constructor so your object is not created yet and thus getC() is not available yet
However you can make the method static and it will work

public Panel03(int size, int sec)
	  {
		 this(size, sec, getC());
	  }
	   static private Color[] getC()
	  {
		 Color[] tc = new Color[2];
		 tc[0] = Color.white;
		 tc[1] = Color.black;
		 return tc;
	  }
	   public Panel03(int size, int sec, Color[] c)
	  {
	  }
}


Was This Post Helpful? 0
  • +
  • -

#5 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:36 PM

Yay, it works, thanks~
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:39 PM

In addition to what Pbl said, wouldn't it be easier to make the Color[] an instance variable rather than using a getter each time you want to access it within the class? Then just have a getter method to return a reference to the Color[]. So something like:
public class Panel3{ 
   //as it is an instance variable, the entire class save for static elements can access it
	private Color[] c;

   //this will be your final constructor in your constructor chain
	public Panel3(..params..){
		c = {Color.black, Color.white};
		..other code..
	 }

	 //return a reference to your Color[]
	 public Color[] getC(){return c;}
}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:45 PM

Since that method is effectively immutable, you might as well declare


private final static Color[] INITIAL_COLORS = { Color.white, Color.black };



and use the variable

Quote

wouldn't it be easier to make the Color[] an instance variable rather than using a getter each time you want to access it within the class?


Making it an instance variable wouldn't of course alter the issue, since it's still in a 'pre-instance' context. Making it a class variable does alter the situation, and making it final underlines the essential immutability of the array

This post has been edited by g00se: 21 January 2010 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,126
  • Joined: 27-December 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:50 PM

View Postg00se, on 21 Jan, 2010 - 06:45 PM, said:

Making it an instance variable wouldn't of course alter the issue, since it's still in a 'pre-instance' context. Making it a class variable does alter the situation, and making it final underlines the essential immutability of the array


Agreed. My point was though that he wouldn't have to include it as a param within his constructors.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 03:57 PM

View Postg00se, on 21 Jan, 2010 - 02:45 PM, said:

Since that method is effectively immutable, you might as well declare


private final static Color[] INITIAL_COLORS = { Color.white, Color.black };



and use the variable

Quote

wouldn't it be easier to make the Color[] an instance variable rather than using a getter each time you want to access it within the class?


Making it an instance variable wouldn't of course alter the issue, since it's still in a 'pre-instance' context. Making it a class variable does alter the situation, and making it final underlines the essential immutability of the array

Agree 100%
Would have done it that way but I wanted to approach the actual problem of invoking an instance method before the object is created
Was This Post Helpful? 0
  • +
  • -

#10 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 05:17 PM

i needed the colors in the constructor because i want to be able to call it with other colors, like:
		 Color[] c = new Color[4];
		 c[0] = Color.blue;
		 c[1] = Color.red;
		 c[2] = Color.green;
		 c[3] = Color.yellow;
		 frame.setContentPane(new Panel03(5, 10, c));

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Making a new array in a call to this

Posted 21 January 2010 - 11:35 PM

View Postvzybilly, on 21 Jan, 2010 - 04:17 PM, said:

i needed the colors in the constructor because i want to be able to call it with other colors, like:
		 Color[] c = new Color[4];
		 c[0] = Color.blue;
		 c[1] = Color.red;
		 c[2] = Color.green;
		 c[3] = Color.yellow;
		 frame.setContentPane(new Panel03(5, 10, c));

does not make sense
how would you change
	 static private Color[] getC()
	  {
		 Color[] tc = new Color[2];
		 tc[0] = Color.white;
		 tc[1] = Color.black;
		 return tc;
	  }


from one call to another ?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Making a new array in a call to this

Posted 22 January 2010 - 04:50 AM

Quote

i needed the colors in the constructor because i want to be able to call it with other colors, like:


In that case, you need something like the following:


import java.awt.*;

public class Panel03 {

	private final static Color[] INITIAL_COLORS = { Color.white, Color.black };

	private Color[] colors;
	private int size;
	private int sec; // NOT a good variable name

	public Panel03(int size, int sec) {
	this.size = size;
	this.sec = sec;
	this.colors = INITIAL_COLORS;
	}

	public Panel03(int size, int sec, Color[] c) {
	this(size, sec);
	this.colors = colors;
	}

}


This post has been edited by g00se: 22 January 2010 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#13 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Making a new array in a call to this

Posted 22 January 2010 - 11:32 AM

View Postpbl, on 21 Jan, 2010 - 10:35 PM, said:

View Postvzybilly, on 21 Jan, 2010 - 04:17 PM, said:

i needed the colors in the constructor because i want to be able to call it with other colors, like:
		 Color[] c = new Color[4];
		 c[0] = Color.blue;
		 c[1] = Color.red;
		 c[2] = Color.green;
		 c[3] = Color.yellow;
		 frame.setContentPane(new Panel03(5, 10, c));

does not make sense
how would you change
	 static private Color[] getC()
	  {
		 Color[] tc = new Color[2];
		 tc[0] = Color.white;
		 tc[1] = Color.black;
		 return tc;
	  }


from one call to another ?


getC() is used in the constructors with 0,1,2 args. getC() is being used for the 3rd arg in the 3 arg constructor where everything happens. if i called panel like Panel03(5, 10) then getC() will happen, but i call Panle03(5, 10, c) and getC() will not happen. getC() does not change.

This post has been edited by vzybilly: 22 January 2010 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Making a new array in a call to this

Posted 22 January 2010 - 11:40 AM

Quote

getC() is used in the constructors with 0,1,2 args.


We know what you're attempting to do. It can't be done - for the reasons given above.
See my example for how you can produce the required functionality
Was This Post Helpful? 0
  • +
  • -

#15 vzybilly  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 160
  • Joined: 24-September 08

Re: Making a new array in a call to this

Posted 22 January 2010 - 02:14 PM

View Postg00se, on 22 Jan, 2010 - 10:40 AM, said:

Quote

getC() is used in the constructors with 0,1,2 args.


We know what you're attempting to do. It can't be done - for the reasons given above.
See my example for how you can produce the required functionality


If it can't be done, how is it working for me right now?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2