Urgent Simple Help Needed


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Urgent Simple Help Needed

  1. #1
    Join Date
    Jan 2005
    Posts
    2

    Urgent Simple Help Needed

    I am trying to learn Java and there are question that i need help on, tried to find answers but they too techinal and dont make sense

    1)what is the difference between public and private?

    2)Why should the attributes of a class be kept private?

    3)What is a constructor?

    4)What is the difference between declaring an object and constructing an object?

    5)What do we use constructors for?

    6)What “value” does an object variable hold?

    7)What syntax must we use to write a constructor?

    8)Where do we define a constructor?

    9)How many constructors can a class have?

    Thanks in advance

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Hi Amak,

    That's a lot of questions ! especially if you are waiting for non technical answers ;-)

    I will answer to your first question (as I'm in a hurry):

    - public means plublic access. A public entity can be accessed/used/modified everywhere in a program (Eg: a public class can be used everywhere in a program)


    - private means private access : That's the complete opposite of the public acess. A private acess states that only the owner of the entity can /access/use/ modify it. (Eg: only the class that declare a private method can actually call it)

    Eventually, the most accurate answer is Here

    Best regards,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  3. #3
    Join Date
    Jan 2005
    Posts
    2
    I thought it was somet similar to that

    anybody else can help me withe others

    Cheers

  4. #4
    Join Date
    Jan 2005
    Posts
    45
    Hi again,

    Well, I'll do one more ;-)

    2)Why should the attributes of a class be kept private?
    The points here are :

    - Security : A private attrribute is guaranted to be never access by any user of your class. So, you can be sure that the attribute used inside your class is always safe ( can't be change outside your class )

    - Separation between implementation and usage: it's crucial to well separate what your class does and how it is used. This way, you can easily optimize how the task is done (better performances) without changing a word to the way users use your class. Typically, private attributes are very close to how the job is really done inside your class. That's why it really makes sense to keep them private (or other strong restriction access)

    Considering all your questions, I suggest you to have a look at the Sun tutorial . It is definitely a good point to start learning Java.

    Best regards,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  5. #5
    Join Date
    Jan 2005
    Posts
    45
    Hi again

    Need more explanations ?... ok , I'll do 2 more today.
    3)What is a constructor?
    4)What is the difference between declaring an object and constructing an object?
    3) A constructor is a special method in a class. This method is always called first when an object is created hence it has simply initialisation purpose.

    4) Declaring an object just state that this object exists somewhere. Constructing an object is allocating memory for storing object datas. The point is : when you declare an object, the object doesn't yet exist (can't be used) until a call to a constructor (with the operator new) or an assignement to a concrete object (object previously created).

    Best regards,
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  6. #6
    Join Date
    Feb 2004
    Posts
    541
    3)What is a constructor?

    This is the portion of code that is run when an object is created.

    4)What is the difference between declaring an object and constructing an object?

    When you declare an object, a certain amount of memory will be set aside for it. The variable that you have declared will be a reference that "points" to that space in memory. There is nothing in that memory though, the object doesn't exist.

    When you create the object, the memory that has been set aside will be used to store the object in. The reference will no longer be null.

    5)What do we use constructors for?

    This is the first piece of code that will run when you create an object. You use it for setting up the object so it meets at least the minimum requirements for it to be able to be used without error.

    6)What “value” does an object variable hold?

    Primitive variables have values, object variables are references to a place in memory that holds all the information contained in the object.

    7)What syntax must we use to write a constructor?

    Assume you have a class called MyClass...a simple constructor would then look like this

    public MyClass()
    {
    //put any legal Java in here
    }

    The constructor doesn't have to be public, it can be private or protected too. You can also define a constructor with parameters.

    8)Where do we define a constructor?

    Anywhere within the class (at class scope, not within other methods).

    9)How many constructors can a class have?

    As many as you want, you can overload constructors just like you can with any method. Only one of the constructors will be run for each object though. If you don't know what method overloading is i'm sure you'll learn about it at a later time.

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    One more....

    Constructors are the only methods in a class that requires the new keyword to be invoked.
    However a class can call one of its own other
    constructor like this:
    Code:
    public class AClass {
      public AClass (String someParameter) {
       .
       .
      }
      public AClass() {  
        this("Default");
       .
       .
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center