Results 1 to 2 of 2
  1. #1
    tinman is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default declare a private instance variable called ran of type random

    hello

    Could someone plese help me with this problem?

    how do I

    declare a private instance variable called ran of type random. Then add a line to the constructor to create an instance of random, using the random class's zero-argument constructor, and directly assign it to the instance variable ran.

    any help would be great thanks :)

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Can you show us your code? Declaring a new Random variable is the same as declaring any other type of variable.

    Likewise, you can create a new instance in your constructor like you would for any other data type.


    I mean, your request details the code steps to take.

    1) declare a private instance variable called ran of type random
    2) add a line to the constructor to create an instance of random
    2a) use the random class's zero-argument constructor
    2b) directly assign it to the instance variable ran.

    Which of the above steps are you having trouble with? Without giving the five line or what not solution, I can't imagine what kind of assistance to give without seeing what code you already have.

    If you present the code you have, we can offer suggestions and help you with any questions you may have.

    Edit:
    In most cases, you shouldn't need an instance variable for Random, but instead should only need a class variable. Is there a reason you need a separate Random instance for each instance of your class?
    Last edited by CodesAway; 12-09-2009 at 01:01 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Array of objects, as an Instance variable
    By blaklite in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 12:56 AM
  2. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM
  3. Instance Variable In Servlet
    By javarishi in forum Java Servlet
    Replies: 3
    Last Post: 06-14-2008, 08:28 AM
  4. Replies: 1
    Last Post: 08-06-2007, 02:17 AM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •