Win a copy of Raising Young Coders: A Parent’s Guide to Teaching Programming at Home this week in the General Computing forum!
Forums Login/signup

basic doubt in program

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send

I agree
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
thanks Smith

it worked

reply
reply
This thread has been viewed 897 times.
Similar Threads
Problem linking button to mouse event - Please help!
Problem with simple drawing tool
using constructors in gui programs
can constructors use 'extends'
Canvas in an appication
More...

All times above are in ranch (not your local) time.
The current ranch time is
Jun 27, 2025 09:06:05.