Re: access Protected Fields from a class which does not have a
constructor
anu wrote:
I have a class called ServiceState that is public class but does not
have a constructor (when I try to use the default constructor in
All classes have a constructor. Do you have the source for
'ServiceState'?
Eclipse, the error is given that the constructor is not visible). This
same class also has some fields that are protected. In order to access
these fields, I created a new class that inherits the ServiceState
class. The protected fields are visible in this class. However, I keep
getting the message that the default constructor is not visible for
the superclass (i.e. ServiceState) and that a constructor needs to be
invoked explicitly. But, I am not quite sure how to do this. I tried
the following:
What is the constructor declaration for 'ServiceState'?
public class ServiceStateProt extends ServiceState {
// public ServiceState(){
You have to name the constructor the same as the class:
public ServiceStateProt()
{
}
/*
You should move all the following logic out of the constructor. It
has no business in there, because its business is not construction.
*/
public void doWork()
{
Intent in = new Intent("SERVICE_STATE_CHANGED_ACTION");
Handler target = new Handler();
Context ctx = null;
PhoneStateIntentReceiver psir = new
PhoneStateIntentReceiver(ctx,target);
psir.onReceiveIntent(ctx, in);
psir.registerIntent();
psir.notifyServiceState(22);
ServiceState ss = psir.getServiceState();
Huh?
Don't create an instance of 'ServiceState' inside an instance of
'ServiceState' or its offspring.
All this statement does is obtain a foreign 'ServiceState' instance
then immediately throw it away.
What did you want it to do, specifically?
}
}
// closing braces on their own line no matter whose brace convention
you follow
Since, getting the intent, then getting the servicestate [sic] from the
Spelling, in particular case, counts.
object of PhoneStateIntentReceiver is how you can get an instance of
the ServiceState class. But, all eclipse asks for is a return type for
the ServiceState method. What am I doing wrong? Please help!
Notice that "Eclipse" (actually, not Eclipse - someone else is
delivering that error to Eclipse - you might investigate who) told you
what you are doing wrong. You declared a method 'ServiceState' in
your class, very badly named since it matches a type name, but did not
provide a return type. All methods must have return types, if only
'void'. Again, the error message was quite accurate.
Speaking of accurate error messages, it is odd that you would
paraphrase the error message instead of quoting it exactly (copy-and-
paste). It is as if you are trying to hamper the efforts of anyone
who wishes to help you.
<http://pscode.org/sscce.html>
--
Lew
Please note - this post contains three requests for information. If
you haven't provided at least three answers, there's a very good
chance that you have not answered those requests.