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

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);

I want to use the object q in the class A. How can i do that?

Recommended Answers

All 10 Replies

You cannot because q is defined in C not in B

You cannot because it is declared private.

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:

C cObj=new C(1,2,3);
cObj.o
cObj.p
cObj.q

If they were public

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?

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

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

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...

public class MyClass{
    static int sharedInt = 0; // package accessible for enclosing classes of MyClass
}
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

BestJewSinceJC probably meant this:

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;

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.

commented: Thanks for pointing that out @_@ +4

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 ).

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'.

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

}

To restate my earlier post, what I said only applies if the variable is static. Otherwise, you need to create a new object in order to refer to 'q' -- which is what javaAddict did. (In other words what javaAddict did is what you have to do if its a public, non-static variable. What I did in my first post is what you'd do for a static variable.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.