For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2005 > Question on Syntax differences between application and applet









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Question on Syntax differences between application and applet
Ken Applequist

2005-02-21, 9:00 pm

I'm trying to teach myself Java from an Intro textbook. The book gives an
example of creating a simple application, and then converting it to an
applet.



It displays a line of output in the application like this:



System.out.println("Welcome to my day!");



My understanding is that "System" is the class, "out" is the object, and
"println" is the method.



In converting the application to an applet they code:



g.drawString("Welcome to my Day!", 200, 70)



I don't understand why there aren't 3 levels specified on this command like
there are on the println command - why don't we have to specify the class?
The book is not at all clear. I'm assuming that g is the object and that
drawString is the method. However, in this case they did not specify a
leading class indicator. Can anyone help me out here?



Thanks,

Ken


Bjorn Abelli

2005-02-21, 9:00 pm


"Ken Applequist" wrote...

> I'm trying to teach myself Java from an Intro textbook. The book
> gives an example of creating a simple application, and then
> converting it to an applet.


It seems like that book isn't too good at explaining the basic OO
concepts...

> It displays a line of output in the application like this:
>
> System.out.println("Welcome to my day!");
>
> My understanding is that "System" is the class, "out" is the object, and
> "println" is the method.
>
> In converting the application to an applet they code:
>
> g.drawString("Welcome to my Day!", 200, 70)
>
> I don't understand why there aren't 3 levels specified on
> this command like there are on the println command - why
> don't we have to specify the class?


> The book is not at all clear. I'm assuming that g is the object and that
> drawString is the method. However, in this case they did not specify a
> leading class indicator. Can anyone help me out here?


The basic building stones in an OO program are the objects to which we send
messages (calling methods).

The objects are instantiated from classes (such as object "g" from class
"Graphics", or "out" from class "PrintStream").

An object can have attributes and methods, and so can some classes, such as
System. The attributes can be of primitive types (int, float, etc) or be
objects (such as "out" in class "System").

Depending on where you are in your application, and what message you want to
send to what object, you send it with different "recievers". You will even
see messages sent "without" recievers, when the reciever is the object or
class itself.

You will also see even longer addressing paths for recievers, when you send
a message to an object that is contained within another object, reachable
only through an accessor method, etc...

A small example to complicate things ;-)


class Klas
{
public void println(String text)
{
System.out.println(text);
}
}

class Test
{
public static Klas k = new Klas();

public static PrintStream out()
{
return System.out;
}

public static void main(String[] args)
{
k.println("Hello");
Test.k.println("Hello");

out().println("Hello");
}
}

The two lines in the main-method does exactly the same thing, but the
reference to "Test" in the second is redundant, as the main-method is
contained in the same class.

The third line sends the message "println" to the object *returned* from the
method "out" in the class Test.

I suggest you look into the basic concepts of objects, classes and sending
messages in another book than the one you're having.


// Bjorn A




Joerg Simon

2005-02-21, 9:00 pm

Ken Applequist wrote:
[snip]
> It displays a line of output in the application like this:
>
> System.out.println("Welcome to my day!");
>
> My understanding is that "System" is the class, "out" is the object, and
> "println" is the method.


yes, thats correct

[snip]
> In converting the application to an applet they code:
>
> g.drawString("Welcome to my Day!", 200, 70)
>

[some things deleted]
> I'm assuming that g is the object and that
> drawString is the method.


yes, thats too correct....

[snip]
> However, in this case they did not specify a
> leading class indicator. Can anyone help me out here?


I hope I can give you a answer which isfy you ^^:

You gernerally don't need something like a loading class. You normally
would yust work with the object.

However, in the case of System, we just have *one* Object, which
represents the standart output. So it is a *design desition* to make
this Object avaliable to the world as a *static member*. Here our Class
is used like an object, is provides us some kind of service.

the g Object normally stands for the individual screen of somewone (hope
this is right, it is a while since i have porigramed applets, but i
think i remember it is the same as the g in swing/awt).
There it is easy to see that there are _many_ objects, and it is *not
possible* to have *all* objects at one place.
So here we use the g *polymorphically*. Means the reference g refers to
some object unknown at compile time, however, the individual screen
object is kind of substituded at runtime...

hope this could help,
feel free to ask further,
and excuse me for my english ^^

Jörg Simon

--
--------------------------------------------------------------
----
||oerg (das ist ein j!)
\|| Simon ICQ: 49154322
\| email: j_simon@sbox.TUGraz.at
dar7yl

2005-02-21, 9:00 pm

"Ken Applequist" <apple07840@yahoo.com> wrote in message
news:mkpSd.28599$ya6.1114@trndny01...
> I'm trying to teach myself Java from an Intro textbook. The book gives an
> example of creating a simple application, and then converting it to an
> applet.
>
> It displays a line of output in the application like this:
> System.out.println("Welcome to my day!");
>
> My understanding is that "System" is the class, "out" is the object, and
> "println" is the method.
>
> In converting the application to an applet they code:
> g.drawString("Welcome to my Day!", 200, 70)
>
> I don't understand why there aren't 3 levels specified on this command
> like there are on the println command - why don't we have to specify the
> class?


The difference here is the 'out' is a static member of the
System class, while 'g' is a local object, accessable
within the current scope.

You don't usually need to specify the class of an object
when accessing a member of it. A static member of a
class is a special case, in which there is only one object
(singleton), and that object's scope is restricted to
the class. Think of it as a global variable, nested within
the class definition.

A non-static member will be created within every object
of a class, and therefore is accessable within that object's
scope.

regards,
Dar7yl


Oscar kind

2005-02-21, 9:00 pm

Ken Applequist <apple07840@yahoo.com> wrote:
> I'm trying to teach myself Java from an Intro textbook. The book gives an
> example of creating a simple application, and then converting it to an
> applet.
>
> It displays a line of output in the application like this:
>
> System.out.println("Welcome to my day!");
>
> My understanding is that "System" is the class, "out" is the object, and
> "println" is the method.


Correct: System.out is the instance of the PrintStream class that points
to the console. These are two levels: the instance reference (System.out)
and the method call. That the first level is itself composed is not
relevant.

Note however, that you cannot create an output stream or writer to the
console yourself. You can only wrap the existing one with (for example) a
DataOutputStream. So to get the concole output stream, you need help of
the System class. That class has a member called "out", which is an
instance of java.io.PrintStream. This member variable is public
(accessible to anyone), static (accessible using the class name) and final
(read-only).


> In converting the application to an applet they code:
>
> g.drawString("Welcome to my Day!", 200, 70)
>
> I don't understand why there aren't 3 levels specified on this command like
> there are on the println command - why don't we have to specify the class?
> The book is not at all clear. I'm assuming that g is the object and that
> drawString is the method. However, in this case they did not specify a
> leading class indicator. Can anyone help me out here?


There are the same levels as before: the instance reference (g) and the
method call. The class name in the previous line is not relevant; only the
instance to call the method on is. In this example, you already have the
instance; in the previous example you didn't (and had to get it from the
System class).


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
Ken Applequist

2005-02-22, 4:02 pm

>>> You don't usually need to specify the class of an object

Ok, but wouldn't this imply that I *could* add a class if I wanted to? I
tried:

paint.g.drawString and I tried Graphics.g.drawstring

and neither of these seemed to work.

Also, does anyone have a recommendation for a good Java book. As was
suggested in another e-mail, I think I need to supplement my library.

Thanks,
Ken

"dar7yl" <no_reply@accepted.org> wrote in message
news:M2qSd.19449$NN.3992@edtnps89...[color=darkred]
> "Ken Applequist" <apple07840@yahoo.com> wrote in message
> news:mkpSd.28599$ya6.1114@trndny01...
>
> The difference here is the 'out' is a static member of the
> System class, while 'g' is a local object, accessable
> within the current scope.
>
> You don't usually need to specify the class of an object
> when accessing a member of it. A static member of a
> class is a special case, in which there is only one object
> (singleton), and that object's scope is restricted to
> the class. Think of it as a global variable, nested within
> the class definition.
>
> A non-static member will be created within every object
> of a class, and therefore is accessable within that object's
> scope.
>
> regards,
> Dar7yl
>
>



Bjorn Abelli

2005-02-22, 4:02 pm


"Ken Applequist" wrote...

>
> Ok, but wouldn't this imply that I *could* add a class
> if I wanted to?


Nope, only if the object you want to pass a message to is a static member of
a class.

> I tried:
>
> paint.g.drawString and I tried Graphics.g.drawstring
>
> and neither of these seemed to work.


As someone pointed out, you can only send messages (call methods) on objects
that are accessible from your code.

As we haven't seen the actual context for your examples, it's not easy to
show how the object and its methods are accessible in this specific case,
but in this case "g" is either a member of the object containing the method
your writing, or a local variable inside the method.

My guess is the latter, where the reference to the Graphics object (g) is
passed as an argument.

// Bjorn A


dar7yl

2005-02-23, 4:01 pm

"Ken Applequist" <apple07840@yahoo.com> wrote in message
news:xrJSd.34113$f%5.27560@trndny03...
>
> Ok, but wouldn't this imply that I *could* add a class if I wanted to? I
> tried: paint.g.drawString and I tried Graphics.g.drawstring
> and neither of these seemed to work.


I'm thinking you don't have a good enough grasp of OO. A class is not
something you can add to an object. A class is the definition. An object
is a concrete manifestation (instance) of a class.

You can call any member of 'g' (which is an object of class Graphics)
by simply dereferencing it. ie, g.drawString()

Static class members is an advanced concept that you can safely ignore
until you have a firm foundation in the basic Object Oriented principles.

> Also, does anyone have a recommendation for a good Java book. As was
> suggested in another e-mail, I think I need to supplement my library.


I would highly recommend "Thinking in Java" by Bruce Eckel
http://www.mindview.net/Books/TIJ/

regards,
Dar7yl


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com