Results 1 to 3 of 3

Thread: Packages

  1. #1
    urbim is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default Packages

    Hi!

    I have a little problem.
    Let's say I have these scripts:
    Java Code:
    package blah;
    
    public class InPack {
    	public boolean pack() {
    		System.out.println("BLAH");
    		return true;
    	}
    }
    Java Code:
    import blah.*;
    
    public class Decrypter {
    	public static void main(String[] args) {
    		boolean pck = pack();
    	}
    }
    First one is in package "blah" and second is in default package.
    I can't acess boolean pack() through imports but I can acess it in this way:
    Java Code:
    blah.InPack x = new blah.InPack();
    boolean pck = x.pack();
    How can I acess pack() trough imports? What am I doing wrong?

    Thanks.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    808
    Rep Power
    6

    Default

    Hi,
    It is not possible to access the method without creating an object.

    Go thru this link
    Lesson: Packages (The Java™ Tutorials > Learning the Java Language)

    and also see the modification below for ur code:

    Java Code:
    import blah.*;
    
    public class Decrypter {
    	public static void main(String[] args) {
                              //Create an object for InPack class
                              InPack obj = new InPack();
                             //Use the above created object to access the method
    		boolean pck = obj.pack();
    	}
    }
    Ramya:cool:

  3. #3
    urbim is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  2. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  3. Using packages
    By prfalco in forum New To Java
    Replies: 5
    Last Post: 01-31-2008, 10:38 PM
  4. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •