• Post Reply Bookmark Topic Watch Topic
  • New Topic

declaring array as public and accessing it in other class  RSS feed

 
bashir adil
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know how to create/declare the array as public in main class and then access it in other class. The few lines of code of my programs are:

--main class()

import java.util.*;
import java.lang.*;

public class enc2kTest
{
public static int[] msgArray = {14555256, 78925654, 76543878, 65432567, 76175544, 923556};
public static void main(String ar[])
{
---------
---------
}
}


----other class accessing array

import java.util.*;
import java.lang.*;

public class inter1
{
public static void main(String ar[])
{
enc2kTest e2k = new enc2kTest();
System.out.println("");
for(int i = 0; i < e2k.msgArray.length; i++ )
{
System.out.print(" | ");
System.out.print(e2k.msgArray[i]);
}
}
}

but it displays the results as zero's.
Kindly help in solving this problem as soon as possible.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Bashir. Always use code tags while pasting code and paste separate classes separately. Makes things even more clear.





Looks better, doesn't it?
 
Cameron Miller
Greenhorn
Posts: 13
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont have to create an instance of enc2kTest to access a static field (array).

Use

enc2kTest.msgArray

instead of

e2k.msgArray

Your program as it is should display the numbers, make sure you are running the correct class when you execute your program.

:-)
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first, there should be only one main() method which is the entry point of the program that you are writing. Get rid of the main() method in the first class file. Then run the second source file. you should get the expected output. What is the output that you are getting on the console?
 
Cameron Miller
Greenhorn
Posts: 13
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote: there should be only one main() method


It doesnt really matter how many main() methods you have, although it is easier to have one. You just need to make sure you are running the right class.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both source files are a part of the same Java application, then it does matter. Every Java application should contain only a single main() method. Otherwise they become two separate Java applications.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would disagree; you can use a main method in each class for testing. There are various ways to prevent its being called, the simplest being to change its access modifier.

Why has nobody commented about the array being public in the first place? Or static? Why is it public? Why is it static? I think calling that array public is a design mistake, and calling it static may be a design mistake.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!