| | |
Another Java Problem with classes
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
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
I want to use the object q in the class A. How can i do that?
cpp Syntax (Toggle Plain Text)
class A{ private int o,p; protected B x; //code; } public class B{ private int o,p; } public class C extends B{ private int q; } //object created in the main C Obj=new C(1,2,3);
You cannot because q is defined in C not in B
You cannot because it is declared private.
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:
If they were public
You cannot because it is declared private.
Java Syntax (Toggle Plain Text)
class A{ private int o,p; protected B bClass; protected C cClass; public void method() { System.out.println(cClass.o+";"+cClass.p); //this is wrong: bClass.q } }
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:
Java Syntax (Toggle Plain Text)
C cObj=new C(1,2,3); cObj.o cObj.p cObj.q
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Sep 2008
Posts: 1,619
Reputation:
Solved Threads: 205
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
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.
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
•
•
•
•
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
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...
java Syntax (Toggle Plain Text)
public class MyClass{ static int sharedInt = 0; // package accessible for enclosing classes of MyClass }
java Syntax (Toggle Plain Text)
public class Main{ public static void main(String... args){ System.out.println(MyClass.sharedInt); // legal if class MyClass and Main are in the same package } }
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.
•
•
•
•
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
Java Syntax (Toggle Plain Text)
C c = new C(); 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
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.
> 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
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.
•
•
•
•
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.
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'.
> 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'.
Java Syntax (Toggle Plain Text)
public class Test { private static double d = Math.PI; // compile time constant expression private static Test me = new Test(); // not a compile time constant expression }
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Differences Between Java and C/C++ (C++)
- For loop problem (Java)
- java problem (Java)
- translate c into java (Java)
- "package" java instruccion in python? (Python)
- Need experienced java help! please! (Java)
- Java III help needed to be done !!! (Java)
- Java Problem with running program (Java)
- Java practice questions (Java)
- java problem with creating arrays (Java)
Other Threads in the Java Forum
- Previous Thread: Link Help Menu to HTML pages
- Next Thread: Random Number Generator Question
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






