I know this is a pretty silly question, but what is exactly an object instance variable? To explain what I mean, take two classes:-

public class Duck {
boolean hasAtail;
double height;

public void go() {
System.out.println(hasAtail + " " + height);
}
}


public class DuckTestDrive {
public static void main(String[] args {
Duck d = new Duck();
d.hasAtail = false;
d.height = 23.0;

d.go();
}
}

That will perfectly compile, but how do I make the main method for this?

public class WhatEver {
Duck m;

public void GO() {
m.go();
}
}

Recommended Answers

All 16 Replies

I know this is a pretty silly question, but what is exactly an object instance variable? To explain what I mean, take two classes:-

public class Duck {
boolean hasAtail;
double height;

public void go() {
System.out.println(hasAtail + " " + height);
}
}


public class DuckTestDrive {
public static void main(String[] args {
Duck d = new Duck();
d.hasAtail = false;
d.height = 23.0;

d.go();
}
}

That will perfectly compile, but how do I make the main method for this?

public class WhatEver {
Duck m;

public void GO() {
m.go();
}
}

check this out:http://en.wikipedia.org/wiki/Instance_variable

and another extra:
An object that belongs to a class is said to be an instance of that class. The variables that the object contains are called instance variables.
When a number of objects are created from the same class, the same copy of instance variable is provided to all. Remember, each time you call the instance the same old value is provided to you, not the updated one .

and also see here:http://forums.techarena.in/software-development/1295736.htm

Quick answer: an instance variable is any variable declared in a class but not inside any block (eg method), and not declared static.
I don't understand your second question - what did you want the main to do?

Nothing about objects there.

check this out

Nothing about objects there.

what did you want the main to do?

I wanted it to call GO().
But for that I need to initialize Duck's instance variables. How do I do that?

Nothing about objects there.

see my edited post, and no i was just explaining to you the theory to your question:what is exactly an object instance variable

Responsibility for initialising a class's instance variables belongs in that class. No other class is expected to know what all those variables are or what initialisation they may need. Duck needs to ensure they are initialised either in their declarations, or in (all) its constrictor), or via an instance initialiser block.

Nothing about objects there.

I wanted it to call GO().
But for that I need to initialize Duck's instance variables. How do I do that?

you'd do something like:

public class Duck {
    private boolean hasAtail;
   private  double height;
public Duck(boolean bool,double h) {
    hasAtail=bool;
    height=h;
}
    public void go() {
        System.out.println(hasAtail + " " + height);
    }
}

and create an instance in another class in the same package by:

Duck d=new Duck(false,23.0);
d.go();

In such a case

public class WETD { //WhatEverTestDrive
public static void main(String[] args) {
WhatEver w = new WhatEver();
w.GO();
}
}

I have to initialize Duck's instance variables too. How do I do that in my class WETD????

In that code you don't refer to Duck in any way at all, so its variables are irrelevant.

In such a case

public class WETD { //WhatEverTestDrive
public static void main(String[] args) {
WhatEver w = new WhatEver();
w.GO();
}
}

I have to initialize Duck's instance variables too. How do I do that in my class WETD????

Please see my above post the code Duck d=.... and d.go() will be in the WETD class which is part of the same package and you'd need to ofcourse change duck to accept a constructor and makes its variables private and assign them via arguments to the constructor

so you mean this code would work out?

public class Duck {
private boolean hasAtail;
private int height;

public Duck(int he, boolean h) {
hasAtail = h;
height = he;
}

public void go() {
\\darn old go() code here
}


public class WE {
Duck d;

public void dostuff() {
d.go();
}
}

public class WETD {
Duck d = new Duck(23, false);

d.dostuff();

forgot to put main() :P

In WE you declare a Duck reference variable, but you never create a Duck, so d will still be null and d.go(); will throw a null pointer exception.
If, in main, you try d = new duck(); that will also fail because there is no constructor in Duck that has no parameters. (If you do not declare any constructors at all Java will give you a default no-args constructor). As soon as you declare Duck(int he, boolean h)... you will no longer get a default constructor. This is a good thing because now there's no way to instantiate Duck without ensuring its vars are initialised.)

I understood everything, thanks all of you!
To cOrRuPtG3n3t!x :-
I saw your home page. Awesome! (and you develop VIRUSES??!!!!??! expert, eh?)

I understood everything, thanks all of you!
To cOrRuPtG3n3t!x :-
I saw your home page. Awesome! (and you develop VIRUSES??!!!!??! expert, eh?)

Lol in my spare time i do have fun defeating the system :P(for educational purposes only)

so you mean this code would work out?

public class Duck {
private boolean hasAtail;
private int height;

public Duck(int he, boolean h) {
hasAtail = h;
height = he;
}

public void go() {
\\darn old go() code here
}


public class WE {
Duck d;

public void dostuff() {
d.go();
}
}

public class WETD {
Duck d = new Duck(23, false);

d.dostuff();

Hmm i was bored so i did this, from what i understand you have 3 classes, WETD,WE,Duck. You want to create an instance of Duck and initialize its instance variables in WETD, Then you want to parse that initiated instance to your class WE? Take a look here:

//WETD.java main class
package wetd;

public class WETD {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Duck d = new Duck(23, false);
        d.go();
        WE we = new WE(d);
        we.dostuff();

    }
}
//Duck.java
package wetd;

public class Duck {

    private boolean hasAtail;
    private int height;

    public Duck(int he, boolean h) {
        hasAtail = h;
        height = he;
    }

    public void go() {
        System.out.println("In duck class: " + hasAtail + " " + height);
    }
}
//WE.java
package wetd;

public class WE {

    private Duck d;

    public WE(Duck duck) {
        d = duck;
    }

    public void dostuff() {
        System.out.println("In WE class, calling go() with same instance as that in WETD");
        d.go();
    }
}
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.