Results 1 to 3 of 3
  1. #1
    sreeram is offline Member
    Join Date
    Dec 2012
    Location
    Hyderabad
    Posts
    4
    Rep Power
    0

    Default Can i use classes defined in different java files but same packages ?

    I am learning java according access specifiers in java. By default i can use classes defined in a java file in an other,
    in practice i am not able to use themm

    i ahve two files
    1)
    Java Code:
    import java.util.Scanner;
    public class DataTypes {
    public static void main(String args[])
    {
      int as[]=new int[11];
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the Number of Integers");
      int n=sc.nextInt(); 
      for(int i=0;i<n;i++)
      {
    	  as[i]=sc.nextInt();
    	  
      }
      int max=as[0];
      for(int i=0;i<n;i++)
      {
          if(max<as[i])
        	  max=as[i];
      }
      System.out.println(max);
      [B]box1 bx=new box1();
    [/B]}
    }


    2)
    Java Code:
    import java.util.Scanner;
    import java2.*;
    
    class box
    {
       	int i,j;
       	box(){}
       	box(int a,int b)
       	{
       		System.out.println("i is equal to "+a);
       		System.out.println("j is equal to "+b);
       	}
    }
    [B]class box1 extends box[/B]
    {
    	
    	box1(int a,int b)
    	{
    		super(2,3);
    		System.out.println(a+2);
    	}
    	public void toMIL(){
    		
    		System.out.println("hhs");
    	}
    }
    public class inheritance {
    public static void main (String args[]){
    	System.out.println("Hello World 1");
    	Scanner sc=new Scanner(System.in);
    	System.out.println("Enter an integer");
    	int s=sc.nextInt();
    	System.out.println(s+2);
    	box1 b1=new box1(4,3);
    	b1.toMIL();
    }}

    how yo use class box1 defined in inheritance.java to be used in datatype.java

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Can i use classes defined in different java files but same packages ?

    What error are you getting?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    MustSeeMelons is offline Member
    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    0

    Default Re: Can i use classes defined in different java files but same packages ?

    Yes you can, you should. Maybe "package" is missing?

Similar Threads

  1. Importing packages and classes
    By tnrh1 in forum New To Java
    Replies: 3
    Last Post: 11-07-2011, 02:35 PM
  2. Create user-defined Classes & Methods
    By lala in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 02:03 PM
  3. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM
  4. Replies: 1
    Last Post: 02-10-2010, 12:55 AM
  5. Classes and packages
    By ramesh.8189 in forum New To Java
    Replies: 4
    Last Post: 02-03-2009, 12:33 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
  •