Communicate between Packages in Java

RedBug

I've a problem that concerns the communication between Packages. This is the question, is it possible to have a list in a package and import this list into another package, so that i can use it there too?

This is the code:

// this is the first package where i create the list and i've create a method to return the       list
package Pack1;
import java.util.*;

public class Lexer 
  {
     public static LinkedList<Object> lst  = new LinkedList<Object>();

     public static LinkedList<Object> getList(){
        return lst;
        }
  } 

    // After this, i add objects to "lst", so the list it's full of objects.
 ///////////////////////////////////////////////////////////////////////////////

//This is the second Package. 
package Pack2;

import Pack1.*;
import java.util.*;

 public class Parse {

//In the main i tried to create a List to contain the old one ("lst") but when i use the 
    //method getList nothing happens! So toString prints nothing.

public static void main(String[] args )
   {
    LinkedList<Object> list = new LinkedList<Object>();
    list = Lexer.getList();
    list.toString();
   }

}
Peter Lawrey

import just allows you to refer to a class, or field without needing to use the full package name. It doesn't give to access to something you didn't have access to already. It just makes your code shorter.

If you want to pass a list from one class to another (whether in another package or not) the simplest approach is to call a method and pass a reference with that list.

What you are doing is using a global field. While that can work it is generally not a good idea as it makes scaling and testing your application harder.

Instead I suggest you avoid mutable static fields, and use a instance field instead. I also suggest avoiding returning a collection. If you need to to string a list add a method like

public String listToString() {
    return list.toString();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Communicate between Packages in Java

From Dev

How to Communicate between two CORBA servers in java

From Dev

Communicate between Java server and PHP client

From Dev

Best way to communicate between java applications running on the same computer

From Dev

How to communicate between the Model and View in a Java FX FXML application?

From Dev

How to communicate between browser and Java Web Start applet

From Dev

how do I communicate between two servers using sockets java

From Dev

Method chaining to communicate between GUI parts in a MVC in Java

From Dev

Communicate between two macros

From Dev

How to communicate between Agents?

From Dev

How to communicate between services?

From Dev

Communicate between UIAutomation and app

From Dev

How to communicate between fragments?

From Dev

Communicate between Controller and ApiController

From Dev

Communicate between two fragments

From Dev

How to Communicate between ViewModels?

From Dev

Communicate between 2 applications

From Dev

How to communicate between javascript modules

From Dev

communicate between python and C++

From Dev

How to communicate between two classes

From Dev

Communicate between two components reactjs

From Dev

Communicate between asyncio protocol/servers

From Dev

Communicate Between Components in React Native?

From Dev

How to communicate between Android and Javascript?

From Dev

Ember communicate between child components

From Dev

Using delegates to communicate between forms

From Dev

Communicate between NodeJS and C++

From Dev

Anylogic - Communicate between two agents

From Dev

Python: Communicate between two files