Another Java Problem with classes

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Another Java Problem with classes

 
0
  #1
Oct 22nd, 2008
Hey all. I have another problem with classes. I have 3 classes A, B and C. B extends C and class A is where objects from B and C will be used. Below is the syntax of what am trying to say
  1. class A{
  2. private int o,p;
  3. protected B x;
  4. //code;
  5. }
  6.  
  7. public class B{
  8. private int o,p;
  9. }
  10.  
  11. public class C extends B{
  12. private int q;
  13. }
  14.  
  15. //object created in the main
  16. C Obj=new C(1,2,3);
I want to use the object q in the class A. How can i do that?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,682
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Another Java Problem with classes

 
0
  #2
Oct 22nd, 2008
You cannot because q is defined in C not in B

You cannot because it is declared private.
  1. class A{
  2. private int o,p;
  3. protected B bClass;
  4. protected C cClass;
  5.  
  6. public void method() {
  7. System.out.println(cClass.o+";"+cClass.p);
  8.  
  9. //this is wrong: bClass.q
  10. }
  11. }

Also none of the above will work because you define o,p,q to be private. They need to be public or have public get methods that return them.

Also:

  1. C cObj=new C(1,2,3);
  2. cObj.o
  3. cObj.p
  4. cObj.q
If they were public
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Another Java Problem with classes

 
0
  #3
Oct 22nd, 2008
Sorry I didnt mention that assuming all the set and get methods are in place. Oh and the q in class C is public not private. Is there a way i can use this q in the class A?
Last edited by joshmo; Oct 22nd, 2008 at 1:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,619
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Another Java Problem with classes

 
0
  #4
Oct 22nd, 2008
If it is public, then you can use

Classname.variableName to refer to it. In this case, that means saying C.q
This only applies if they are in the same package, which in your case, they are. If they were in a different package, you could say packageName.Classname.Variablename
Last edited by BestJewSinceJC; Oct 22nd, 2008 at 1:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: Another Java Problem with classes

 
0
  #5
Oct 22nd, 2008
Originally Posted by BestJewSinceJC View Post
If it is public, then you can use

Classname.variableName to refer to it. In this case, that means saying C.q
This only applies if they are in the same package, which in your case, they are. If they were in a different package, you could say packageName.Classname.Variablename
the variable q is not declared in the class A and I dont want to declare it. Is there anyway this can be done? I tried that and it says "non-static variable cannot be referenced from a static context" If also someone could share with me what this means. Thanks alot
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Another Java Problem with classes

 
0
  #6
Oct 23rd, 2008
Originally Posted by joshmo View Post
the variable q is not declared in the class A and I dont want to declare it. Is there anyway this can be done? I tried that and it says "non-static variable cannot be referenced from a static context" If also someone could share with me what this means. Thanks alot
static values are resolved at compile time, before objects are created.

A static member of a class is a shared location in memory between objects of that class.

Because static values are resolved at compile time before an object is created, and all objects of the class share the value, you have the right [with the appropriate access modifier] to access that shared address between objects of the class without needing an actual object to qualify the member to access the member.

For example...

  1.  
  2. public class MyClass{
  3. static int sharedInt = 0; // package accessible for enclosing classes of MyClass
  4. }

  1.  
  2. public class Main{
  3.  
  4. public static void main(String... args){
  5.  
  6. System.out.println(MyClass.sharedInt); // legal if class MyClass and Main are in the same package
  7.  
  8. }
  9. }

Notice that in main of a different class file, I didn't need to create an object of MyClass in order to access sharedInt - the address of the reference-variable sharedInt is resolved at Compile Time and all MyClass objects have access to it.

When you call C.q the compiler is forced to believe q is a static member in class C, but if it does not see the static modifier by q then you will receive a compile-time error.

If q is not a static member, then although each C class object has q, they have their own individual address for their reference variable and therefore to access q you need to give a hint to the compiler (or runtime) of which particular C object you want to qualify q with to access the appropriate value of q for whatever purpose you need to access it for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,682
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Another Java Problem with classes

 
0
  #7
Oct 23rd, 2008
Originally Posted by joshmo View Post
the variable q is not declared in the class A and I dont want to declare it. Is there anyway this can be done? I tried that and it says "non-static variable cannot be referenced from a static context" If also someone could share with me what this means. Thanks alot
BestJewSinceJC probably meant this:

  1. C c = new C();
  2. c.q;

That is how non static variables are accessed.
You can do the above anywhere you want (provided that q public)

And again, this can be done:
C c = new C();
c.q;
c.o;
c.p;


This NOT:

B b = new B();
b.q;
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Another Java Problem with classes

 
1
  #8
Oct 23rd, 2008
A lot of complicated terminology being thrown around here but a few points:

> A static member of a class is a shared location in memory between objects of that class.

Don't mix concepts and implementation details when explaining. From the JLS:
If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized
> static values are resolved at compile time, before objects are created.

A compile time constant expression when assigned to a static field[or not] is evaluated at compile time; a static field is incarnated when the class is initialized. There is no *static value*.

> Notice that in main of a different class file, I didn't need to create an object of MyClass in
> order to access sharedInt - the address of the reference-variable sharedInt is resolved at
> Compile Time and all MyClass objects have access to it.

`sharedInt' is a variable of Primitive Type and not Reference Type.

> If q is not a static member, then although each C class object has q, they have their own
> individual address for their reference variable and therefore

Each instance created has its own set of the state/member variables. There is no *individual address for their reference variables*.

> to access q you need to give a hint to the compiler (or runtime) of which particular C
> object you want to qualify q with to access the appropriate value of q for whatever
> purpose you need to access it for

There is no *hinting* or *forcing*; the code just satisfies the syntactic and semantic rules along with working as intended.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Another Java Problem with classes

 
0
  #9
Oct 23rd, 2008
Originally Posted by ~s.o.s~ View Post
A lot of complicated terminology being thrown around here but a few points:

> A static member of a class is a shared location in memory between objects of that class.

Don't mix concepts and implementation details when explaining. From the JLS:


> static values are resolved at compile time, before objects are created.

A compile time constant expression when assigned to a static field[or not] is evaluated at compile time; a static field is incarnated when the class is initialized. There is no *static value*.

> Notice that in main of a different class file, I didn't need to create an object of MyClass in
> order to access sharedInt - the address of the reference-variable sharedInt is resolved at
> Compile Time and all MyClass objects have access to it.

`sharedInt' is a variable of Primitive Type and not Reference Type.

> If q is not a static member, then although each C class object has q, they have their own
> individual address for their reference variable and therefore

Each instance created has its own set of the state/member variables. There is no *individual address for their reference variables*.

> to access q you need to give a hint to the compiler (or runtime) of which particular C
> object you want to qualify q with to access the appropriate value of q for whatever
> purpose you need to access it for

There is no *hinting* or *forcing*; the code just satisfies the syntactic and semantic rules along with working as intended.
Yes, I am very sorry for mixing static fields and members. Fields execute once and only once when the first object of the type is created (otherwise if a program executed all static fields of all objects at compile time it would not be a very good thing X_X ).
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,628
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Another Java Problem with classes

 
0
  #10
Oct 23rd, 2008
Bad terminology yet again.

> Yes, I am very sorry for mixing static fields and members.

Members of a class include fields, methods and nested classes and interfaces. So the statement, *mixing static fields and members* doesn't make much sense.

> Fields execute once and only once when the first object of the type is created

Fields don't *execute*, they are initialized. Static fields are initialized when the class is first loaded / initialized.

> otherwise if a program executed all static fields of all objects at compile time it would not
> be a very good thing X_X

At compile time, the `compile time constant expressions' are evaluated irrespective of whether the LHS is a static field or not. The Java Language Specification has rules laid down for what exactly are `compile time constant expressions'.
  1. public class Test {
  2.  
  3. private static double d = Math.PI; // compile time constant expression
  4.  
  5. private static Test me = new Test(); // not a compile time constant expression
  6.  
  7. }
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC