• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Re Usable Code

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic