Anurag Singh
Anurag Singh

Reputation: 727

use of object to access the class variables and methods

In the question which i am posting i want to know that in order to access the setused()method i create a object named obj1 of array type for class alpha. but if i want to access the same method from class beta and than i would have to create another objects. But is there any way so that i can use the same object ie. obj1 of array type to access the setused() method

import javax.swing.JOptionPane;
public class first()
{
      public void main(String args[])
     {
         alpha[] obj1=new alpha[5];
         for(int i-0;i<5;i++)
         obj1[i].setused(int 1);
     }
}

class alpha
{
   private int un_used=5;
   private int used=0

   public int getun_used()
   {
          return un_used;
   }

   public int getused()
   {
          return used; 
   }

   public void setused(int use)
   {
          used= use;
   }
}

class beta
{
    private String name;
}

Upvotes: 0

Views: 90

Answers (4)

Rossiar
Rossiar

Reputation: 2564

You would want to use the static modifier on the method you wanted to access. This would mean that in other classes you can access the method using ClassName.methodname(parameters). So for instance if you wanted to have setused be a static method, then you would access it in first by alpha.setUsed:

public static void setUsed(int use) {
    used=use;
}

public class first {

    public void main(String args[]) {
        alpha.setUsed();
    }
}

If you want to find out more about static and other subjects then head to the Java Tutorials (http://docs.oracle.com/javase/tutorial/), they are pretty good at explaining it all.

You are also initialising 5 objects, not 1. To have the class alpha behave like an array would require you to have alpha extend Array.

public alpha extends Array {}

This will allow the class to use all the methods of Array via inheritance. Note that another word for inheritance is polymorphism and has been mentioned above.

Also, the objects in the array have to be instantiated now that you have initialised them.

public void main(String args[]) {

    alpha[] obj1=new alpha[5];
    for(int i-0;i<5;i++) {
        obj[i] = new alpha();
        obj1[i].setused(1);
    }
 }

Objects have to be initialised (given memory space) and then instantiated (filling the memory space with an actual object).

Notice how objects are usually called:

alpha a = new alpha();

That is initialisation and instantiation on one line, here it is on two lines:

alpha a;
a = new alpha();

The first line allocating memory for a, the second actually creating a and placing it in that memory, ready for use.

You have only initialised the array of objects, so there would be a NullPointerException when you tried to use them.

Hope this helped.

Upvotes: 2

Bhaskar
Bhaskar

Reputation: 7523

  class beta{
private String name;
private alpha[] alphaArray ;

public void setAlpha(alpha[] alphaArray){
  this.aplhoaArray = alphaArray.clone();
}

public void someMethod(){
  // access alpha[index].setUsed() method here 
}

}

Not sure I understand your problem correctly , but maybe this code will help.

Upvotes: 0

Himanshu Mohta
Himanshu Mohta

Reputation: 1043

You have to pass the reference of obj1 to the class beta like:
class beta
{
   alpha a;
   beta(alpha a)
   {
     this.a = a;
   }


}

Upvotes: 0

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340783

First of all your code will throw NullPointerException because creating an array of objects of a given size doesn't mean you are creating that many objects. The array only holds nulls.

Secondly your alpha and beta classes do not share any methods, there is no setused() method in beta. Since now I am assuming there is:

class beta
{
    private String name;

    public void setused(int use) { //...
    }

}

If you want to use the same reference to access that method of both classes, the cleanest way would be to use :

interface Used {

    void setused(int use);

}

abstract base class would also do. Then implement that interface:

class alpha implements Used { //...
class beta implements Used { //...

Now create an array of Used type:

 Used[] obj1=new Used[5];
 obj1[0] = new alpha();
 obj1[1] = new beta();
 //...
 for(int i-0;i<5;i++)
 obj1[i].setused(1);

Finally read about Java naming conventions.

Upvotes: 3

Related Questions