java - Object initialized in the main class and using the same object in a method/function for different purpose? -


i have class contains following coding

import java.util.scanner; import java.io.*;  public class someclass {      public static void main(string[] args) {         product[] p= new product[3];         p[0] = new product();         p[1] = new product();         p[2] = new product();         p[0].name="john";    // product class contains variable string name;                                // , method getname() returning name;         p[1].name="tony";         p[2].name="abraham";         someprintmethod();     }      public static void someprintmethod() {         for(int =0;i<3;i++) {             system.out.println(p[i].getname());         }     } } 

so question can use object defined in main class in other method other

purpose in same class or there method use object because trying

do , giving me error , cannot figure out how it.

you can make p static variable, declared outside of main:

static product[] p = new product[3]; 

but should of course first ensure doing makes sense in context of program.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -