Hello everyone, i was reading java tutorials on the internet then i saw this program,

class Cat {
	
    private String mCatName;

    Cat(String name) {
        mCatName=name;
    }

    public String getName() {
        return mCatName;
    }

    public void setName(String strName) {
        mCatName = strName;
    }
	
	    public void messWithCat(Cat kitty) {
        kitty = new Cat("Han");
    }

    public void changeKitty(Cat kitty) {
        kitty.setName("Wookie");
    }

    Cat haveKitten()
    {
        Cat kitten = new Cat("Luke");
        return kitten;
    }

  }

class abc {
	public static void main (String args[]){
	
	Cat cat1 = new Cat("Jabba");
Cat cat2 = new Cat("Leia");

cat1.getName();    
cat2.getName();    
messWithCat(cat1);
changeKitty(cat2);
Cat cat3 = haveKitten();
cat1.getName();    
cat2.getName();    
cat3.getName();    


	}  
}

inside class Cat,
what is this ?

public void messWithCat(Cat kitty) {
        kitty = new Cat("Han");
    }

    public void changeKitty(Cat kitty) {
        kitty.setName("Wookie");
    }

    Cat haveKitten()
    {
        Cat kitten = new Cat("Luke");
        return kitten;
    }

they look like objects but how they have been created in the same class ? and please explain what is this ?

also when i run this program i am getting this error,


E:\Java\bin>javac abc.java
abc.java:41: cannot find symbol
symbol : method messWithCat(Cat)
location: class abc
messWithCat(cat1);
^
abc.java:42: cannot find symbol
symbol : method changeKitty(Cat)
location: class abc
changeKitty(cat2);
^
abc.java:43: cannot find symbol
symbol : method haveKitten()
location: class abc
Cat cat3 = haveKitten();
^
3 errors

please explain this.

Ezzaral commented: You won't get much help if you give negative rep to those who try to answer you. -3

I think the problem with your code is You didn't use a instance of the class Cat to call those methods.. Following would make sense...

class abc {
	public static void main (String args[]){
	
	Cat cat1 = new Cat("Jabba");
Cat cat2 = new Cat("Leia");

cat1.getName();    
cat2.getName();    
cat1.messWithCat(cat1);
cat1.changeKitty(cat2);
Cat cat3 = cat1.haveKitten();
cat1.getName();    
cat2.getName();    
cat3.getName();    


	}  
}

what is this ?

public void messWithCat(Cat kitty) {
        kitty = new Cat("Han");
    }

    public void changeKitty(Cat kitty) {
        kitty.setName("Wookie");
    }

    Cat haveKitten()
    {
        Cat kitten = new Cat("Luke");
        return kitten;
    }

they look like objects but how they have been created in the same class ? and please explain what is this ?

messWithCat , changeKitty , haveKitten Are not objects.. they are methods.. So there's no problem to create method inside a class.. when you created a instance of a class using new keyword then you have an Object of this class..

commented: that is not what i've asked , stope increasing your post count -1
commented: No reason for Xufyan to be a jerk here when his question is nearly incomprensible. +15
commented: phaaaa +1 too +8

Yes messWithCat , changeKitty , haveKitten are methods.(Cat kitty) is a constructor.....

(Cat kitty) is a constructor.....

I believe you meant to say is a parameter.

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.