Search code examples
javanetbeansclient-servergetter-setter

Get variable from another class within another project


I have 3 project. (and Ignore all Connection to database and Registry to Computer). Project A = API (java library), Project B = Java Class (Server) , Project C = Java Class (Client) .

in Project A I have 1 java class for entity , lets say (entity.java)

private Boolean data;
private String name;    
public void Data_set(Boolean data){
this.data = data;
}
public void Name_set(String name){
this.name = name
}        
public Boolean Data_get() {
return data;
}
public String Name_get() {
return name;
}

in Project A, I have 1 Interface , lets say (interface.java)

public void method();

in Project B 1 java class which implements interface.java, lets give it a name (server.java) which import all java & interface class on project A.

public void method() {
entity Entity = new entity();
Entity.Data_set(true);
Entity.Name_set("Someone");
}

in Project C 1 java main class, lets give it a name (main.java) which import all java & interface class on project A.

server Server = new server();
Server.method();
entity Entity = new entity();
System.out.print("Boolean = "Entity.Data_is();
System.out.print("Name = "Entity.Name_get();

WHAT I WANT. After I run "main.java" It should be below

Boolean = true
Name = someone

but it show difference value it show below.

Boolean = false
Name = null

but if i set entity on "main.java" not in server.java the result there are no problem.

NOTE for connection database and connection between client-server there are no problem.

Is there any solution for this.

Thanks.


Solution

  • It sounds as if you create an Entity in the Server class:

    class Server implements Interface {
        private Entity entity;
    
        public void method() {
            entity = new Entity();
            entity.setName(...);
            entity.setData(...);
        }
    }
    

    But rather than print the details of that entity, you create a new one and print the details to that one (which you never assign it any details):

    class Main {
        public static void main(String[] args) {
            Server server = new Server();
            server.method();
    
            Entity entity = new Entity();
            //print entity
        }
    }
    

    What you should be doing is grabbing the entity from the server:

      class Main {
        public static void main(String[] args) {
            Server server = new Server();
            server.method();
    
            Entity entity = server.getEntity();
            //print entity
        }
    }
    
    class Server {
        private Entity entity;
    
        public void method() {
            entity = new Entity();
            entity.setName(...);
            entity.setData(...);
        }
    
        public Entity getEntity() {
            return entity;
        }
    }
    

    As you may have noticed, I used a different style of capitalization than you used. The capitalization style I used is the coding convention people typically use when writing Java. Language have their own styles, it's best to stay consistent with the language's choice of style.