Results 1 to 13 of 13
  1. #1
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Random.nextGaussian() and static

    At the beginning of my program code is the statement:
    "import java.util.Random;"
    A method that is not static calls "Random.nextGaussian()",
    but the compiler stops and gives the message:
    "non-static method nextGaussian() cannot be referenced from a static context"
    This must be an error in the java system.
    How could it be my error?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default Re: Random.nextGaussian() and static

    Quote Originally Posted by hill0093 View Post
    At the beginning of my program code is the statement:
    "import java.util.Random;"
    A method that is not static calls "Random.nextGaussian()",
    but the compiler stops and gives the message:
    "non-static method nextGaussian() cannot be referenced from a static context"
    This must be an error in the java system.
    How could it be my error?
    No, it's no an error in the java system; have you checked the API documentation for the Random class? Is that method a static method?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    Quote Originally Posted by JosAH View Post
    No, it's no an error in the java system; have you checked the API documentation for the Random class? Is that method a static method?

    kind regards,

    Jos
    Read my question again. I think you misunderstood it.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default Re: Random.nextGaussian() and static

    Quote Originally Posted by hill0093 View Post
    Read my question again. I think you misunderstood it.
    No I did not: that method isn't static so you can't call it that way.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    I'll try again:
    A method that is not static calls "Random.nextGaussian()",
    but the compiler stops and gives the message:
    "non-static method nextGaussian() cannot be referenced from a static context"
    The question is: how can the call be from a static context?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,786
    Blog Entries
    7
    Rep Power
    21

    Default Re: Random.nextGaussian() and static

    The fact that your method isn't static has nothing to do with it; you're trying to call that method from the Random class from a static context.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    Ok, so if it's not the method, then what should I look for that might make the context static?
    It's a small program, and I don't see any static context except main() which static context I think I have escaped.

  8. #8
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    I think I found it, Thanks. I was blind. It's called by a static method in another external class.
    Sorry about being so dumb.

  9. #9
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    I am back to programming again after another task. I am surprised that
    taking the call to Random.nextGaussian() out of the static OuWid.sR() did not
    get rid if the compiler message:
    "non-static method nextGaussian() cannot be referenced from a static context".
    The following code also gives the message, and I don't see any static.
    for(int i=0;i<nRandCols;i++)if(ugStg.charAt(i)=='u') {
    double unif01=Math.random(); ouStg+=(OuWid.sR(unif01*2+5,6)+",");
    } else { double gaus01=Random.nextGaussian(); ouStg+=(OuWid.sR(gaus01+6,6)+","); }
    What do I look for? Random.nextDouble() also gave a message, but I changed it to Math.random().

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Random.nextGaussian() and static

    For the love of all things holy, please use code tags with your posted code. As it stands your code is impossible to read and understand.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Random.nextGaussian() and static

    In answer to your question about what makes a context static, in expressions like "Random.nextDouble()" and "Math.random()" it the class names (in bold) that introduce static contexts. In effect the expressions translate into "The result of calling the Random class's nextDouble() method" and "the result of calling the Math class's random() method".

    But it is really only specific instances of Random that have a nextDouble() method you can call. Ie it is an ordinary, nonstatic, method. So you would write something like:

    Java Code:
    Random random = new Random();
    
    //... later
    double value = random.nextDouble();
    Notice that "random.nextDouble()" uses a variable, not a class name. The bold part does not introduce a static context.

    To avoid the error use static methods with class names, and non static ones with variables (or other expressions).
    Last edited by pbrockway2; 03-26-2013 at 03:50 AM.

  12. #12
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Random.nextGaussian() and static

    Thank you pbrockway2, very nice explanation

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Generate weighted random numbers, nextGaussian ()
    By graympa in forum New To Java
    Replies: 2
    Last Post: 03-25-2011, 10:36 PM
  3. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

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