Simple packaging problem (I hope)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Simple packaging problem (I hope)

  1. #1
    Join Date
    Oct 2003
    Posts
    3

    Simple packaging problem (I hope)

    I have 2 java source files, a.java and b.java. I have a bunch of useful methods in b.java that I want to include in a.java without actually typing them in. I assume I need to package b.java somehow and import it into a.java. Any ideas on going about this? Thanks.

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    I have a bunch of useful methods in b.java that I want to include in a.java without actually typing them in.
    It depends what you mean. Do you mean you want to be able to call those methods? Or do you mean what wan those methods available by code using a.java?

    It also helps if you give a description of what a.java and b.java represent.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Oct 2003
    Posts
    3
    Sorry that I wasn't specific enough, I'm fairly new to Java.

    I want to call the methods from b.java.

    For example:

    b has a method that is called input(String message), which uses the swing input dialog and returns a String.

    I know I could just import swing into a.java and use JOptionPane.showInputDialog("text"), but I'd rather use something like b.input("some text").

    a.java is just some small command line program, so the user would call it by using "java a".

    Hope that could clarify the situation more.


  4. #4
    Join Date
    Mar 2003
    Posts
    834

    Arrow

    If these two files are in the same folder you don't have to do anything. Here's a rough example:
    Code:
    C:\david\docs\forums\import>ls *.java
    A.java  B.java
    As you can see I have A.java and B.java in the same folder. Here is the contents of B.java:
    Code:
    public class B {
    
            public B() {
            }
    
            public String input(String message) {
                    return "User's Input";
            }
    }
    ...and here is A.java:
    Code:
    public class A {
    
            private B b;
    
            public A() {
                    b = new B();
                    String userInput = b.input("What is your favouriate colour?");
            }
    }
    Because these two files are in the same folder (and they don't use package statements) there is no need to use an 'import' statement.
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Oct 2003
    Posts
    3
    Worked Perfectly! Thanks so much!

    I didn't realize it would be that easy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center