File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "instance variables" Watch "instance variables" New topic
Author

instance variables

srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26

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

Joined: Feb 07, 2005
Posts: 102

Here is the class structure



I hope now you can say that Class B has Instance Variable A
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Does this really belong to SCJP.
I guess not, but no offenses though.


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
kshitij dogra
Ranch Hand

Joined: Dec 28, 2008
Posts: 39
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.


SCJP 5.0 - 100%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
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

Joined: Aug 16, 2007
Posts: 1374
Lack of reading...
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
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

Joined: Oct 13, 2008
Posts: 362
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


SCJP5 and SCWCD1.5
Think Twice Act Wise...
kshitij dogra
Ranch Hand

Joined: Dec 28, 2008
Posts: 39
yup sure campbell,

i am relatively new to javaranch.

Still learning the ways though
srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26
Thanks Kshitij dogra. iam pleased by your answer .i understand what you are saying .Thanks a lot :lol:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: instance variables