• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

question 1 of 2 - different parts of a class?

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've gone through 2 java books so far and I'm starting on my 3rd. However, I'm running into problems with comprehension of topics and I think it's because I was never crystal clear on the different parts of a class. Some things I have down like when I declare printer p = new printer(); means that p is a handle that points to allocated memory for the object p? However, there are other parts that are still confusing to me. I was wondering if someone who knows this stuff inside and out, could put in a basic class and comment out beside each part what it is called. Like, in a class have a statement that shows an instance member followed by //instance member? This in particular I'm stuck on. So, basically, if anyone can help me get a better grasp on the language fundamentals and vocabulary, thanks a lot!!!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
whatever i have got of your query,i am replying that.
we will take the 2nd part first.
//instance member
//-this is a comment entry in java which is totally ignored by the compiler .
in the book this has been given for u to understand that it is an instance variable
//-is a one line comment entry
multiple lines comment entries are written thru
/*
this would
be ignored by the
compiler
*/
i hope that was ur query
now coming to the first part
when we say
print p=new print();
this can be written in 2 steps as well
1)print p;
2)p=new print();
the first line just creates a variable p of class type print.
here as we know a class is nothing but a user defined data type
so p is a variable of type print.here it is not referring to anything.it is just a variable pointing to null.
in the 2nd line
p=new print();
now an object has been created and the reference p is referring to that object of type print.
it is important to understand that there is a difference b/w a reference and an object.
whenever an object is created,the constructor of the specific class is called and memory is allocated.
the reference just points to that object and whenever we use any fn. or variable of that class ,we do that by using the reference of that object.
i hope i have given a valid reply.
ranch hands ,please correct me if i am wrong or incomplete
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tyler here you go:
public class circle{
public circle(){}; // 1.this is the constructor
public void draw(){
System.out.println("draw");
} //2. this is a method. each circle object
// you create will get its own copy of this
// method. When it's called. "draw" will
// be shown at the output.
}
Above is a class definition of a circle object. This is the blueprint to create objects of type circle. This above class will compile fine, but nothing will happen when you try to run this because there is no main function. Not every class needs a main function. But if you would like to create new circles, you'll need a main function. So take a look at this.
public class circle{
public circle(){}; // constructor
public void draw(){
System.out.println("draw");
} // here is your method definition

public static void main(String args[]){
circle red= new circle();
red.draw();
}
}
Now this new class includes the main function, and within it we create a new circle object named red. At the line circle red= new circle(); this calls the circle constructor and creates a circle object that is referenced by the variable red, and this object has a method named draw. When we call the draw method with red.draw(); the string "draw" is shown on the output. The important thing to note is that you can only create new objects of type circle in the main function. The rest of this is just a class definition of an object type circle. I'm sorry if this confuses you. I hope it helps.
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must not have posted my question correctly, but unfortunately, right now I can only think of two terms that I'm having a problem understanding. What is an instance member and what is a static member? My book says:
Static members can be accessed by both the class name and objects of the class, but instance members can only be accessed by objects of the class.
This is where I'm getting stuck. I figure there are terms like this that are fundamental to the language that I first need to understand their meaning before being able to grasp larger concepts. Thanks for trying to help!
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe I can help.
A static member is also known as a class member. Everybody in the class can use a static member but there is only one copy of the static member, so it is shared.
An instance member is a member in every class. So every new object gets its own instance member. So in my definition above, the method draw is a public method, so every new instance of the class circle, gets its own draw method.
But if it were a static method, then it is shared throughout the class. Each time a new circle is created, it wouldn't come with a draw method since its' a static member. But an instance of a class can still access a static method as long as it is public. So if we created say five circle objects: red, blue, green, and black, there would be one draw method shared between all of them, instead of each of them having there own.
Now there is one important point about a static member. A static member can be accessed without instantiating a new object, which is a big benefit.
I hope this helps. I know that the concepts are a little confusing. What books are you referring to?
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sean Casey:
An instance member is a member in every class. So every new object gets its own instance member. So in my definition above, the method draw is a public method, so every new instance of the class circle, gets its own draw method...


So an instance member is a method? And each new instance gets it's own copy of that method (instance member)?


...Now there is one important point about a static member. A static member can be accessed without instantiating a new object, which is a big benefit.
I hope this helps. I know that the concepts are a little confusing. What books are you referring to?


I'm unclear on what you're trying to say about "A static member can be accessed without instantiating a new object." I'm using Learn Java in 21 Days and A Programmer's Guide to Java Certification. Thanks a lot for your help. I really appreciate this!

[This message has been edited by tyler jones (edited December 23, 2000).]
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
an instance member can be a method or a variable. Here's another simple program:
public class circle{
circle(){};
static int j; //1. static variable.
// of all the objects instantiated
// or created, this will be the only
// copy of j. They will share it
// amongst themselves.

public void draw(){
System.out.println("draw");
} //2. this is not static and each
// instance of class circle will
// get its own copy of this method.

public static void change(int j){
System.out.println("change");
} //3. this is a static member.
// of all the circles created,
// they will share this one copy
// of the method change.
public static void main(String args[]){//4.main function
circle red= new circle();
circle blue= new circle();
circle yellow= new circle();

}
}
okay I hope this helps a static member can be a variable or a method. red, blue, and yellow all have their own draw methods, but they all share the one static copy of variable j, and the one copy of the static method change(). In order to access the draw method, you must create an instance of the class first, so only red, blue, and yellow can draw. But you can access the static method change or the static variable j, without instantiating(creating) an instance of the class.
if someone wants to use the draw method there is only one way to do that, and that is to create a new circle object and have that object invoke the method. But anyone can invoke the public static method, even somebody outside the class.

[This message has been edited by Sean Casey (edited December 23, 2000).]
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tyler maybe this will help
this is the way Bruce Eckel defines the static keyword in his book Thinking in Java:
"When you say something is static, it means that data or method is not tied to any particular object instance of that class. So even if you've never created an object of that class you can call a static method or access a piece of static data." Chapter 2 p.117.
I don't know much about the book you're reading, but you can go to www.bruceeckel.com and download Thinking in Java, and read what I've referenced above. It's a really good book. I bought it, and I think it's definitely worth the value. I hope some of this helps you. Let me know if you still have any problems.
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sean. You've cleared it up for me I think. As far as Bruce Eckel's book, I did get it and after the first 4 chapters, it just all seemed to go too far over my head, so I went to the Certification book. This one seems much clearer. Maybe when I get a stronger foundation in the language and understand the core concepts better, I'll go back and read Thinking In Java again. Thanks again!
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do the same thing you do: when one book goes over my head I pick up another. I have five books on the language, three books on graphics, and two on certification. So if you need any recommendations I could probably help you out, and save you some money. Take care.
 
author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just published a book entitled Beginning Java Objects, by Wrox Press, Ltd. It provides a basic introduction to object concepts, object modeling, AND Java programming, something that few if any other books attempt. It takes a single case study through development of a UML model and rendering it into Java code. Click on the link in my signature to check it out on Amazon.com!
Regards,
Jacquie Barker
------------------
author of:
Beginning Java Objects
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic