• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance variables

 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here how can we say that class b's instance variable is a ?
if yes how can we say like that ?

few are saying that class b has instance variable declared as the non primitive type a.IS That true ?

my question is since class b doesnt contain any main method whats the use of saying like that? since we cant run and what sthe use of saying like that


can you say this with a small example?



 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is the class structure



I hope now you can say that Class B has Instance Variable A
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does this really belong to SCJP.
I guess not, but no offenses though.
 
kshitij dogra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class MAIN
{
public static void main(String[] args)
{
xyz ob = new xyz();
System.out.println(ob.o.x);
}
}

class abc
{
int x = 10;
}

class xyz
{
abc o = new abc();
}


Hope you get the meaning when I say xyz class, has an instance variable, which is actually an object reference to class abc.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were right that it's not an SCJP-related post.

I detect confusion in this thread. I also detect poor style. Please start your class names with CAPITAL letters LikeThis. And don't start variable names with capital letters. Then you can see which is the Type (Starts With Capital Letter) and which is the primitive (int boolean char short long float double or byte) or the variable (doesn't start with capital letters). Also avoid using "A" "B" or "XYZ" as names; they are not easy to read.
Also make sure to indent your code and use the code button. If you did those things, your posts would be easier to read.

In the first example there is a field called "obj" which is of type "a", and Kshitij Dogra demonstrated how you can use that variable. Creating methods which use that variable and a constructor to set the variable would be more useful design; that would show how the class can use its included data.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lack of reading...
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth mycherla wrote:
here how can we say that class b's instance variable is a ?
if yes how can we say like that ?

We can say like that, its java.
It shows a Has-A relationship
srikanth mycherla wrote:
few are saying that class b has instance variable declared as the non primitive type a.IS That true ?

That's absolutely true. Class b has instance variable a which is non primitive.
srikanth mycherla wrote:
my question is since class b doesnt contain any main method whats the use of saying like that? since we cant run and what sthe use of saying like that

There is not much use of this class. But for explanation about instance variables it is good enough.
If you really want methods in your class you are free to add them to it.(Who's stopping you)

Check all the messages and you should understand this clearly. Also, please listen to Campbell, use code buttons and also the coding conventions....


 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes absolutely it is an istance variable of class-B

and it is following HAS-A relationship because you are using A class reference variable as an instance variable in class B
 
kshitij dogra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup sure campbell,

i am relatively new to javaranch.

Still learning the ways though
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kshitij dogra. iam pleased by your answer .i understand what you are saying .Thanks a lot :lol:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic