-6

I want to create just an object,i am not sure if i am creating and it is right.My example ,lets say i have 2 class(Userinput and paper).0fcourse there is and the main.In this example No inherit (just 2 simple class).Am i create an object right?How i put in it in an array or in the same array?

package exercise;
public class Exercise{
    static int N; //from keyboard .I have a class userinput.It doesnt need to write it here ,i have in the other class the problem

    public static void main(String[] args) { // main class

        Paper[] pin = new Paper[N]; //i create an array
        Paper.setpencil(3); // i wrote the 3 .In this way i create 3 pencil?
        Paper.getpencil(3); 

        Paper.setsomething(4); // i wrote the 4 .I create 4 ?
        Paper.getsomething(4); 

    } }
public class Paper{ //in this class i am confused
    public Paper(){} //default constructor 

    private int pencil;
    private String something; 

    public int getpencil(){
        return pencil;
    } 
    public void setpencil(){
        pencil=UserInput.getInteger():
    }
    public int getsomething(){
        return something;
    }
    public void setsomething(){
        something=UserInput.setInteger();
    }
}  
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Let your code be compilable is the first step of the process. Actually this code will not compile for many reasons. – Davide Lorenzo MARINO Sep 15 '16 at 10:00
  • 5
    Please provide a [mcve], properly formatted. The code you've shown at the moment shouldn't compile, as `setpencil` etc are *instance* methods but you're calling them on the `Paper` type as if they were static methods. Additionally your "constructor for `Paper` is called `paper`, so that wouldn't compile either. None of that part has anything to do with arrays. I'd suggest tackling one thing at a time. – Jon Skeet Sep 15 '16 at 10:00
  • Possible duplicate of [How can I add new item to the String array?](http://stackoverflow.com/questions/14518195/how-can-i-add-new-item-to-the-string-array), just type difference in this case – px06 Sep 15 '16 at 10:00
  • There is a lot of things wrong with your code, including compile-time errors, I suggest using IDE like Eclipse, it's gonna help you a lot. – Shadov Sep 15 '16 at 10:02
  • 1
    Please indent your code properly if you are asking people to try and read it. – khelwood Sep 15 '16 at 10:03
  • where is wrong? and the http://stackoverflow.com/questions/14518195/how-can-i-add-new-item-to-the-string-array isnt the same as i wrote .Sorry you want to write the constructor .OK i edit –  Sep 15 '16 at 10:04
  • Notice the statement : `Paper[] pin = new Paper[N]`. `N` is uninitialized. – progyammer Sep 15 '16 at 10:08
  • you take it from keyboard thats why.. –  Sep 15 '16 at 10:12

1 Answers1

2

with this statement:

Paper[] pin = new Paper[N];

you create an array of object of class Paper.

You must also create an object for each array element like:

for (int i=0; i < N, i++)
{
    pin[i] = new Paper();
}

And next you should refer to an element (e.g. first element that with index 0) of the array in this way:

pin[0].setpencil(3);
aurox
  • 107
  • 1
  • 3
  • 8