• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

access variable in constructor.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help with the following code:

I don't know why <code>this(y)</code> does not compile?
Please help, thanx in advance.
Guoqiao
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found it will work, if you write like this:
public class test {

int y = 0;
public test(){
//this(y), Compiler Error!
}
public test(int x){
this();
y = 1;
}
}
I think compiler must insert super() in that "Compiler Error"
place.
Error Message: You cannot use y before parent class not initial.
------------------
I love MySQL!
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, if I uncomment the <code>this(y);</code>, the error is as:
<code>
Test.java [19:1] Can't reference y inside this() call
this(y); //not compile

^
1 error
</code>
Please give explanation.
Guoqiao

Originally posted by pcedar:
I found it will work, if you write like this:
public class test {

int y = 0;
public test(){
//this(y), Compiler Error!
}
public test(int x){
this();
y = 1;
}
}
I think compiler must insert super() in that "Compiler Error"
place.
Error Message: You cannot use y before parent class not initial.


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Guoqiao Sun:
[B]Please help with the following code:

I don't know why this(y) does not compile?
Please help, thanx in advance.
Guoqiao


Compiles no since it is illegal to reference things like that.
(read the compiler output!)
Why dont you do like this:

But then again we still dont gain anything from here.

------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Antti,
As you have shown in your code :
<pre>
public class Test {
int y;
public Test() {
this(0);
}
public Test(int val) {
this.y = val;
}
}
</pre>

The advantage of using this(0) is to initialize the property(attribute) of Test object to a default value if the client chooses not to specify the value of y.I have used this often to do some initialization of my object before calling its methods.
Hope this helps,
Sandeep
SCJP2, OCSD(JDeveloper), OCED(Oracle Internet Platform)
 
Oh. Hi guys! Look at this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic