Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Static in a non static

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Static in a non static

    Hey!

    The the code that i have must be able to, via a method, take an array of numbers and make them into a string and then return to print them out.
    There is two parts of this code so far (two methods other than the main).

    The first method adds numbers in an array and prints out the sum and it works fine.
    But the second one, aka the string one, doesn't work.


    Any tips?


    	public static void main(String[] args) {
    		// First part!
    		int[] Array = { 3, 4, 5, 6, 7 };
    		int hellfire = sum(Array);
    		System.out.println(hellfire);
     
    		// Second part...
    		System.out.println(toString());
     
    	}
     
    //Works! 
    	public static int sum(int[] Array) {
     
    		int n = 0;
    		for (int i : Array) {
    			n += i;
    		}
    		return n;
    	}
     
    //Can't get it to do anything.. 
    	public static String toString(int[] k) {
    		int[] n = { 3, 4, 5, 6, 7 };
    		String str = Arrays.toString(n);
    		System.out.println("n = " + str);
    		return str;
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    doesn't work.
    Please explain. If there are error messages, copy the full text and paste it here.

    //Can't get it to do anything..
    How are you calling the method? Where is the code that passes it an int array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    Please explain. If there are error messages, copy the full text and paste it here.


    How are you calling the method? Where is the code that passes it an int array?

    I get "Cannot make a static reference to the non-static method toString() from the type Object".
    I thought that i could call it from "System.out.println(toString());" in the main, but i guess that is a wrong way of doing it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    Cannot make a static reference to the non-static method toString()
    To call a non-static method you need a reference to an instance of the class.
    System.out.println(toString());  // calls default toString method 
     
    System.out.println(toString(Array));  // calls the toString(int[]) method that is static

    Read about method overloading - possible to have more that one version of a method with the same name as long as the arguments are different.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    This is what my task says I should be able to do.


    The method String toString (int [] arr) that builds a string and it prints out the content of the array.
    It should be able to be used as follows with good results:

    int[] n = {3,4,5,6,7};
    String str = Arrays.toString(n);
    System.out.println("n = " + str);

    I just don't get what i should be doing here... Maybe anyone in here knows? Becuse even when i changed it and did as you told me to do
    then it just crashes.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    it just crashes.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Those 3 lines of code in post#5 work for me.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Those 3 lines of code in post#5 work for me.



    Exception in thread "main" java.lang.StackOverflowError
    at km222nb_lab3.Arrays.toString(Arrays.java)
    at km222nb_lab3.Arrays.toString(Arrays.java:32)
    at km222nb_lab3.Arrays.toString(Arrays.java:32)




    	public static String toString(int[] k) {
    		int[] n = { 3, 4, 5, 6, 7 };
    		String str = Arrays.toString(n); //Says that something is going on here...
    		System.out.println("n = " + str);
    		return str;
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    Exception in thread "main" java.lang.StackOverflowError
    at km222nb_lab3.Arrays.toString(Arrays.java)
    at km222nb_lab3.Arrays.toString(Arrays.java:32)
    at km222nb_lab3.Arrays.toString(Arrays.java:32)
    The code is calling itself repeatedly until the callstack exceeds the available memory.

    You should NOT give your class the same name (Arrays) as a java SE class: java.util.Arrays.
    Change the name of your class to something else.
    The Arrays class's toString method is a wellknown and used method for displaying the contents of arrays. Don't confuse things by using the same class name.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    The code is calling itself repeatedly until the callstack exceeds the available memory.

    You should NOT give your class the same name (Arrays) as a java SE class: java.util.Arrays.
    Change the name of your class to something else.
    The Arrays class's toString method is a wellknown and used method for displaying the contents of arrays. Don't confuse things by using the same class name.

    Well that's good to know however.. i am not allowed to use any of the help from the lib.
    And on top of that i think that i should be able to get the info from the n array and not from the
    static one, aka from the one in which the toString method exists.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    i should be able to get the info from the n array and not from the
    static one, aka from the one in which the toString method exists
    Sorry, I have no idea what you are asking.

    Can you post the code and a description of what it is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    Sorry, I have no idea what you are asking.

    Can you post the code and a description of what it is supposed to do?
    Yeah.
    Been altering the code much too from what they wanted from post 5.

    public class arraykm {
    	static int[] arr = { 3, 4, 5, 6, 7 };
     
    	public static void main(String[] args) {
     
    		int hellfire = sum(arr);
    		System.out.println(hellfire);
     
    		// The main method is here as a tester.
    		// So it tests out the other static methods.
    		// The second static method should be able to print
    		System.out.println(toString(arr));
    		//But it doesn't work.. no matter what i do. 
    	}
     
    	public static int sum(int[] arr) {
     
    		int n = 0;
    		for (int i : arr) {
    			n += i;
    		}
    		return n;
    	}
     
    	public static String toString(int[] arr) {
    		String str = arr.toString();
    		System.out.println("n = " + str);
    		return str;
    	}
     
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    What happens when the code is compiled? Any errors?
    If no errors, what happens when the code is executed?

    What is the toString method supposed to do? Given the array: int[] arr = { 3, 4, 5, 6, 7 };
    What should be in the String the method returns?
    What code is needed to create the desired String?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Static in a non static

    Jumping in here at the last moment and I would also like to see what the assignment is. It is difficult to help or see if you're going down the wrong path unless we know what you're trying to accomplish.

    Regards,
    Jim

  14. #14
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Well lets start back at the begining then....
    I have this class that i need to make a couple of static methods, i am not allowed to use the lib.
    The main method is there to act as a testprogram to show how each method can be used.
    The first method is done but it is the second one that i have difficultiy with...

    The second one says: Make the method String toString(int[] arr) that builds up a string and that when it prints out it prints the content
    of the array. It should be used in the following way with good results:

         int[] n = {3,4,5,6,7};
         String str = Arrays.toString(n);
         System.out.println("n = " + str);

    Forget about the codes above then. Lets just pretend that we have a clean main method and want to make a
    new method from the info from the second question.
    Any ideas of how i should make this work?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    Any ideas of how i should make this work?
    What should the method return? Given this array: int[] arr = { 3, 4, 5, 6, 7 };
    What should be in the String the method returns?
    Post what the desired String would look like for that array.

    You can not write any code Until you decide what should be in the String.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    What should the method return? Given this array: int[] arr = { 3, 4, 5, 6, 7 };
    What should be in the String the method returns?
    Post what the desired String would look like for that array.

    You can not write any code Until you decide what should be in the String.
    I want to get the numbers from the array without touching any lib and pass them to a string
    and print them out.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    Please post the exact String that the method should return for this array: int[] arr = { 3, 4, 5, 6, 7 };
    For example: "34567"

    Once we get that we can work on how to build that String.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    Please post the exact String that the method should return for this array: int[] arr = { 3, 4, 5, 6, 7 };
    For example: "34567"

    Once we get that we can work on how to build that String.
    Lets just say this. I want a code that takes the info from an array, can be static or in main, and prints it out from its String method when we call for it.
    Say take this one step at the time. And i will get to the end result when i get there because this is the best way for me to learn. Otherwise i don't..

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    You need to decide what String the toString method will return and then work on the code to build that String.
    I've asked for a sample String to show what you want the method to return.
    Without that nothing can be done.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    n = 3 4 5 6 7. Thats what it should print out!

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    Ok so the String returned by the toString method should have the elements of the array separated by a single space.
    To do that write a loop that iterates though the array and concatenates each element of the array and a space to a String.
    Define the String before the loop and assign it an empty String: "".
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    120
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Static in a non static

    Quote Originally Posted by Norm View Post
    Ok so the String returned by the toString method should have the elements of the array separated by a single space.
    To do that write a loop that iterates though the array and concatenates each element of the array and a space to a String.
    Define the String before the loop and assign it an empty String: "".
    Thanks, i got it working now!
    Atleast i think so haha.



    	public static String tostring(int[] arr) {
    		String str1 = null;
    		String strk = " "; 
    		for (int i = 0; i < Arr.length; i++) {
    			System.out.print(Arr[i] + strk);
    			str1 = Integer.toString(i);
    		}
    		return str1;


    In main i say
    	System.out.println(tostring(Arr));

    And i use "Arr" as a static method alone with the numbers 34567.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,315
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Static in a non static

    What does that version of code output to the console?
    Why does the method have a print statement inside of it? What happens when you comment out the print statement that is inside of the method?
    What is the definition of what the toString method is supposed to do?
    Print a String
    or
    return a String?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)
    By Rexoa in forum Java Theory & Questions
    Replies: 6
    Last Post: March 2nd, 2014, 06:34 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM