File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes basic doubt in program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic doubt in program" Watch "basic doubt in program" New topic
Author

basic doubt in program

linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
import java.awt.*;

import java.awt.event.*;


class MyCanvas extends Canvas

{

public void paint(Graphics g)

{ g.setColor(new Color(255,0,0));
g.fillOval(5,5,20,20);


g.setColor(Color.blue);

g.fillOval(40,10,20,20);


g.setColor(Color.yellow);

g.fillRect(10,20,40,50);


}



}


public class TestCanvas

{
Frame f;
void Launchmc()
{

MyCanvas mc=new MyCanvas();

f=new Frame("drawings");
f.add(mc);

f.setSize(300,200);
f.setVisible(true);

f.addWindowListener(
new WindowAdapter(){ public void windowClosing(WindowEvent e)
{ System.exit(0);
}

});


}


public static void main(String args[])

{



TestCanvas tc=new TestCanvas();
tc.Launchmc();


}

}

Why is it that I can make object of MyCanvas class in Launchmc() method
and also access that object from Launchmc(),MyCanvas class is not public
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
By not defining an access level the class receives default access, which means that it is accessible by classes in the same package. Since you did not have any package declarations for either class they are both in the default package. Since they are in the same package TestCanvas can access MyCanvas.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
thanks



if i shift MyCanvas class to other .java file
[that file will contain only MyCanvas class]

will this java program TestCanvas9.java work

both files will be in same folder
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39868
    
  28
Please use more informative thread titles. You want people who are interested in your problem to look at your thread, so they need to know what it is about.
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44

I agree
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
linus dale wrote:thanks



if i shift MyCanvas class to other .java file
[that file will contain only MyCanvas class]

will this java program TestCanvas9.java work

both files will be in same folder


As long as you don't put either of the classes in different packages it should work.
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
thanks Smith

it worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic doubt in program