• Post Reply Bookmark Topic Watch Topic
  • New Topic

Re Usable Code  RSS feed

 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hear everyone talk about Java reusable code.

I always right my programs from scratch.

How do I use pieces of other programs that are not related to the current one I am writing. I use Eclipse. How would I call like Sql connection from a previous program so I don't have to re-write the connection string in my current program I am writing. I am just curious how this is done.

Thank You
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally the reusable portions of your code are packed into JARs and the JARs are placed in your classpath. You then can import any of the classes in the JAR into your application.

When using an IDE you normally have to configure the classpath differently than you do for command-line tools. So look up Eclipse's documentation to see how to add a JAR (or an external project) to a Project's class path.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to do it inside Eclipse is to have one or more projects with reusable code, and put those on the build path of your application projects.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think about the java API. There is tons of code there that can be used by anyone, at any time. Basically, you write a class that has the methods you want, compile it, then call them like the Math class or the String class methods.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I understand this correctly.

I would build a jar or a class and put it in my class path.

Once in my classpath (java build path same)

I would just reference it like a method

So if I called it Justin.java or jar
[code]
private void Justin(String string) {
}
[code/]
Say it was a SQL connection string. Then my password and login would be hidden. Just by entering this method I would connect to the database?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not exactly...

to use a Math class method, you have to explicitly tell it the class and method:



if your methods are static, you would have to do the same thing:


if the methods aren't static, like for a String, you need to make the object, and then call the methods on the object:



or

 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin v = new Justin("hello");
int k = v.justingsMethod("db2");

So if my connection String is called


This should then call the connection string from the class I added to the classpath?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!