Results 1 to 2 of 2
  1. #1
    t0mat0 is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Unhappy Doubt in Package-Object creation

    Hi.. This is my first file: Protection.java

    Java Code:
    package p1;
     
    public class Protection {
    	int n =1;
    	private int n_pri=2;
    	protected int n_pro=3;
    	public int n_pub=4;
    	
    	public Protection(){
    		
    		System.out.println("Base Constructor:");
    		System.out.println("n: "+n);
    		System.out.println("n_pri: "+n_pri);
    		System.out.println("n_pro: "+n_pro);
    		System.out.println("n_pub: "+n_pub);
    	}
    }
    This is my Second File:SecondPackage.java under the same package p1

    Java Code:
    package p1;
     
    class SamePackage {
    	Protection p1= new Protection();
     
    		
    	
    		
    		System.out.println("Same Package Constructor");
    		System.out.println("n: "+p1.n);
    		//System.out.println("n_pri: "+p1.n_pri);
    		System.out.println("n_pro: "+p1.n_pro);
    		System.out.println("n_pub: "+p1.n_pub);
    		
    	
     
    }
    and here is the Test File:
    Java Code:
    package p1;
     
    public class Demo {
    	
    	public static void main(String args[]){
    	
    	Protection Ob1= new Protection();
    	Derived Ob2= new Derived();
    	SamePackage Ob3= new SamePackage();
    	}
     
    }
    My doubt is why System.out.println Statements are shown as error here?. I know if i put the System.out.println statements inside SamePackage constructor it wont show any error... but I want to know why --

    Java Code:
    System.out.println("Same Package Constructor");
    System.out.println("n: "+p1.n);
    //System.out.println("n_pri: "+p1.n_pri);
    System.out.println("n_pro: "+p1.n_pro);
    System.out.println("n_pub: "+p1.n_pub);
    cant be put without a constructor in class SamePackage?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Cross posted and already answered, not that a cross poster would bother to inform this forum of that.
    New To Java - Doubt in Package-Object creation

    db

Similar Threads

  1. String Object Creation
    By indranil in forum New To Java
    Replies: 1
    Last Post: 04-13-2010, 11:30 AM
  2. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  3. object creation
    By enygma in forum Java Applets
    Replies: 0
    Last Post: 01-05-2010, 10:01 AM
  4. Object creation and construstion
    By abimaran in forum New To Java
    Replies: 8
    Last Post: 12-15-2009, 09:58 AM
  5. Object creation and memory issues
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:56 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
  •