Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basic doubt in program

 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50248
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I agree
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Smith

it worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic