Hey

Lets see if I can explain myself correctly......

I have 2 projects, one a program and another a library.

First project is basically this, more or less (obviously it is more complicated)

//This is the main project

//Inside com.stuct:
//Global.java
package com.stuct;



public class Global
{
    public static int var=4;
}


//Main program
//Inside com.program:
//main.java

package com.program;

public class main 
{
       public static void main(String[] args) 
 	{
          //In order to access that I currently use something like:
           System.out.println(Global.var);

         }

}

Thats currently my main program. Now here is the library:

//This is the library
//This is a different project than the other
//Inside com.something:
//Things.java

package com.something;

public class Things
{
   public static void main(String[] args) 
	{
             //HERE IS MY MAIN QUESTION. CAN I DO THIS:
             System.out.println(Global.var); 
         }

}

In the section "CAN I DO THIS",is where lies my problem. Could I add that library to my main project in its build path and would the instruction I try to use in my library work? Or do I have to create the same structure with Global in my library (which to me doesnt make sense as I would be using 2 different variables even if named the same).

Thanks!

Recommended Answers

All 9 Replies

what do you need two main methods for?
I don't know what your vision about a "library" is, but AFAIK, a class library has no use for a main method.

what do you need two main methods for?
I don't know what your vision about a "library" is, but AFAIK, a class library has no use for a main method.

It was a example out of my head quickly......
Here is what I ment:

//This is the library
//This is a different project than the other
//Inside com.something:
//Things.java 
package com.something; 
public class Things
{   
   public void printastring()
 	{             //HERE IS MY MAIN QUESTION. CAN I DO THIS
                             System.out.println(Global.var);        
  } 
}

if everything's imported, I don't see the problem.
do you have trouble with it?

if everything's imported, I don't see the problem.
do you have trouble with it?

What I ment was that in each project (both the lib and the main program) do I have to have that com.stuct package and that Global class with var?

I believe that I should only have it in the main program (project) and not in the library project at all.

Hey

Lets see if I can explain myself correctly......

I have 2 projects, one a program and another a library.

First project is basically this, more or less (obviously it is more complicated)

//This is the main project

//Inside com.stuct:
//Global.java
package com.stuct;



public class Global
{
    public static int var=4;
}


//Main program
//Inside com.program:
//main.java

package com.program;

public class main 
{
       public static void main(String[] args) 
 	{
          //In order to access that I currently use something like:
           System.out.println(Global.var);

         }

}

Thats currently my main program. Now here is the library:

//This is the library
//This is a different project than the other
//Inside com.something:
//Things.java

package com.something;

public class Things
{
   public static void main(String[] args) 
	{
             //HERE IS MY MAIN QUESTION. CAN I DO THIS:
             System.out.println(Global.var); 
         }

}

In the section "CAN I DO THIS",is where lies my problem. Could I add that library to my main project in its build path and would the instruction I try to use in my library work? Or do I have to create the same structure with Global in my library (which to me doesnt make sense as I would be using 2 different variables even if named the same).

Thanks!

Hey im not to sure what you mean, but no1 as stultuske said your library wouldnt have a main method? it would more contain methods for a specific task or multiple tasks etc on that note i dont think its good to go orinting messages etc from a library that should be done by the class calling it via return variables etc... take a look here:

/*
 * main class
 *P1.java
 */
package p1;

public class P1 {

    public static void main(String[] args) {
        library.setString();
        System.out.println(library.getString());
        System.out.println(library.hello);
        library.saySomething("Hello from lib");
        
    }
}

and here is the library:

/*
 * library
 *library.java
 */
package p1;

public class library {

    public static String hello;

    static String getString() {
        return hello;
    }
    static void setString() {
        hello = "Hello world";
    }
    static void saySomething(String say) {
        System.out.println(say);
    }
}

hope that helps your question/problem

What I ment was that in each project (both the lib and the main program) do I have to have that com.stuct package and that Global class with var?

I believe that I should only have it in the main program (project) and not in the library project at all.

Yes you must keep that package structure in both and no the Global.var is only when referencing to the variable in the library and yes it should only be done in the main... use get and set methods to return variables etc from the library. a library should also not use other classes global variables. instead they should be sent by the main class as arguments to a method in the library, its a much safer option

The main function was a mistake and showing a string is simply a example. Please understand that :)

Yes you must keep that package structure in both and no the Global.var is only when referencing to the variable in the library and yes it should only be done in the main... use get and set methods to return variables etc from the library. a library should also not use other classes global variables. instead they should be sent by the main class as arguments to a method in the library, its a much safer option

From what you have posted and in my situation, then I think I would only need it in the main project: One package with one class with variables. Then with the libary I can access those variables.

If there is a misunderstand, Ill post more code samples to see if I can express correctly the idea.

What I ment was that in each project (both the lib and the main program) do I have to have that com.stuct package and that Global class with var?

No, having it in one place would be fine. Let's suppose that you have the `Global' class in the library project (called for e.g. jlib). To then use it in your other project (called jAwesome), you'd:
* Import the Global class in any class of the jAwesome project
* Make sure your dependencies are set right. If you are using an IDE (e.g. Eclipse), you can make a project depend on another project
* If you are compiling from command line, make sure you use the -cp switch of `javac` command to specify the jar file of the library project.

No, having it in one place would be fine. Let's suppose that you have the `Global' class in the library project (called for e.g. jlib). To then use it in your other project (called jAwesome), you'd:
* Import the Global class in any class of the jAwesome project
* Make sure your dependencies are set right. If you are using an IDE (e.g. Eclipse), you can make a project depend on another project
* If you are compiling from command line, make sure you use the -cp switch of `javac` command to specify the jar file of the library project.

Yeah, messing around with the code earlier I thought if I used those public variables in the library, I could use them as globals all around the program.

Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.