private and static error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IHateSuperman

    private and static error

    public class StaticField2{
    public static void main(String args[]){
    private int x, y; // <<== error 1
    for ( y = 0 ; y < 100 ; y++){
    x = StaticMethod();
    System.out.prin tln(" x = "+x);
    }
    }
    public static int StaticMethod(){
    private static int m = 0; // <<== error 2
    m = m++;
    return m;
    }
    }

    why i can't declare x and y as a private?
    why i cant declare m as private static?

    New to Java(OOP?) world.
    TIA.


    --
    "If we don't stand together than this is the end"
    The Business

  • SPG

    #2
    Re: private and static error

    Hi,

    OK, Here is your solution:

    <SNIP YOUR CODE>
    public class StaticField2{
    public static void main(String args[]){
    private int x, y; // <<== error 1 (SPG: All variables declared inside a
    method are private to that method. JAVA does not allow you to modify this.)
    for ( y = 0 ; y < 100 ; y++){
    x = StaticMethod();
    System.out.prin tln(" x = "+x);
    }
    }
    public static int StaticMethod(){
    private static int m = 0; // <<== error 2 (SPG: Cannot keep a static
    variable inside a method)
    m = m++;
    return m;
    }
    }
    </SNIP>

    So, To acheive your solution I would imagine you need to do this:

    public class StaticField2{
    private static int m = 0; // <= Static variable declared global to the
    class only.. All methods have access to the single instance

    public static void main(String args[]){
    int x, y; //<= BOTH ARE LOCAL VARIABLES TO THE MAIN METHOD
    for ( y = 0 ; y < 100 ; y++){
    x = StaticMethod();
    System.out.prin tln(" x = "+x);
    }
    }
    public static int StaticMethod(){
    m = m++;
    return m;
    }
    }

    I hope this helps you..

    Steve

    "IHateSuper man" <[email protected] > wrote in message
    news:40220703_1 @news.tm.net.my ...[color=blue]
    > public class StaticField2{
    > public static void main(String args[]){
    > private int x, y; // <<== error 1
    > for ( y = 0 ; y < 100 ; y++){
    > x = StaticMethod();
    > System.out.prin tln(" x = "+x);
    > }
    > }
    > public static int StaticMethod(){
    > private static int m = 0; // <<== error 2
    > m = m++;
    > return m;
    > }
    > }
    >
    > why i can't declare x and y as a private?
    > why i cant declare m as private static?
    >
    > New to Java(OOP?) world.
    > TIA.
    >
    >
    > --
    > "If we don't stand together than this is the end"
    > The Business
    >[/color]


    Comment

    • Karl von Laudermann

      #3
      Re: private and static error

      IHateSuperman <[email protected] > wrote in message news:<40220703_ [email protected] y>...[color=blue]
      > public class StaticField2{
      > public static void main(String args[]){
      > private int x, y; // <<== error 1
      > for ( y = 0 ; y < 100 ; y++){
      > x = StaticMethod();
      > System.out.prin tln(" x = "+x);
      > }
      > }
      > public static int StaticMethod(){
      > private static int m = 0; // <<== error 2
      > m = m++;
      > return m;
      > }
      > }
      >
      > why i can't declare x and y as a private?
      > why i cant declare m as private static?[/color]

      Because x, y, and m are local variables in a method. Keywords
      "private" and "static" can only be applied to member variables. A
      local variable is already private, in that it's impossible to access
      it from outside the method it is declared in. "static" doesn't even
      make sense for a local variable; if you're looking for the same
      functionality of the "static" keyword in C when applied to a local
      variable, declare a member variable instead. Of course, the member
      variable will be accessible from all methods of the class, not just
      StaticMethod. This should work:

      public class StaticField2{
      private static int m = 0; // private static class member

      public static void main(String args[]){
      int x, y; // Local variables
      for ( y = 0 ; y < 100 ; y++){
      x = StaticMethod();
      System.out.prin tln(" x = "+x);
      }
      }
      public static int StaticMethod(){
      m = m++;
      return m;
      }
      }

      Comment

      • SPC

        #4
        Re: private and static error

        Hoping it's help!
        I am sorry if it's doesn't.

        //Start
        package Private;

        public class Private{

        static int x, y; // <<== error 1 //ok
        private static int m = 0; // <<== error 2 //ok
        // declare sth as private / private static in "Class" but out side "main"
        and "fonction" //


        public static void main(String args[]){

        for ( y = 0 ; y < 100 ; y++){
        x = StaticMethod();
        System.out.prin tln(" x = "+x);
        }
        }
        public static int StaticMethod(){

        m = m++;
        return m;
        }
        }
        //END




        "IHateSuper man" <[email protected] > a écrit dans le message de
        news:40220703_1 @news.tm.net.my ...[color=blue]
        > public class StaticField2{
        > public static void main(String args[]){
        > private int x, y; // <<== error 1
        > for ( y = 0 ; y < 100 ; y++){
        > x = StaticMethod();
        > System.out.prin tln(" x = "+x);
        > }
        > }
        > public static int StaticMethod(){
        > private static int m = 0; // <<== error 2
        > m = m++;
        > return m;
        > }
        > }
        >
        > why i can't declare x and y as a private?
        > why i cant declare m as private static?
        >
        > New to Java(OOP?) world.
        > TIA.
        >
        >
        > --
        > "If we don't stand together than this is the end"
        > The Business
        >[/color]


        Comment

        Working...