You're correct that your issues are rooted in not understanding static. There are many resources on this, but suffice to say here that something static belongs to a Class whereas something that isn't static belogns to a specific instance. That means that

public class A{
    public static int b;

    public int x;
    public int doStuff(){
        return x;
    }

    public static void main(String[] args){
        System.out.println(b); //Valid. Who's b? A (the class we are in)'s b.
        System.out.println(x); //Error. Who's x? no instance provided, so we don't know.
        doStuff(); //Error. Who are we calling doStuff() on? Which instance?

        A a = new A();
        System.out.println(a.x); //Valid. Who's x? a (an instance of A)'s x.
    }
}

So related to that your method histogram isn't static, so you need an instance to call it. You shouldn't need an instance though; just make the method static:

Change public String histogram(int randomNum) to public static String histogram(int randomNum).

With that done, the line histogram(randomNum); becomes valid. However, you'll still get an error on System.out.print(histogram + "\n");, because histogram as defined here is a function, not a variable. This is related to the return statement. When something says return x (for any value of x), it is saying to terminate the current method call and yield the value x to whoever called the method.

For example, consider the expression 2 + 3. If you were to say int x = 2 + 3, you would expect x to have value 5 afterwards. Now consider a method:

public static int plus(int a, int b){
    return a + b;
}

And the statement: int x = plus(2, 3);. Same here, we would expect x to have value 5 afterwards. The computation is done, and whoever is waiting on that value (of type int) receives and uses the value however a single value of that type would be used in place of it. For example:

int x = plus(plus(1,2),plus(3,plus(4,1)); -> x has value 11.

Back to your example: you need to assign a variable to the String value returned from histogram(randomNum);, as such:

Change histogram(randomNum) to String s = histogram(randomNum).

This will make it all compile, but you'll hit one final roadblock: The thing won't run! This is because a runnable main method needs to be static. So change your main method to have the signature:

public static void main(String[] args){...}

Then hit the green button!

Answer from Mshnik on Stack Overflow
🌐
CodeWithHarry
codewithharry.com › tutorial › java-string-methods
String Methods | Java Tutorial | CodeWithHarry
length(): This method is used to find the length of a string. ... public class StringExample { public static void main(String[] args) { String quote = "To be or not to be"; System.out.println(quote.length()); } } ... public class StringExample { public static void main(String[] args) { String quote = "To be or not to be"; System.out.println(quote.indexOf("be")); // index of text System.out.println(quote.indexOf("r")); // index of character } }
Top answer
1 of 3
4

You're correct that your issues are rooted in not understanding static. There are many resources on this, but suffice to say here that something static belongs to a Class whereas something that isn't static belogns to a specific instance. That means that

public class A{
    public static int b;

    public int x;
    public int doStuff(){
        return x;
    }

    public static void main(String[] args){
        System.out.println(b); //Valid. Who's b? A (the class we are in)'s b.
        System.out.println(x); //Error. Who's x? no instance provided, so we don't know.
        doStuff(); //Error. Who are we calling doStuff() on? Which instance?

        A a = new A();
        System.out.println(a.x); //Valid. Who's x? a (an instance of A)'s x.
    }
}

So related to that your method histogram isn't static, so you need an instance to call it. You shouldn't need an instance though; just make the method static:

Change public String histogram(int randomNum) to public static String histogram(int randomNum).

With that done, the line histogram(randomNum); becomes valid. However, you'll still get an error on System.out.print(histogram + "\n");, because histogram as defined here is a function, not a variable. This is related to the return statement. When something says return x (for any value of x), it is saying to terminate the current method call and yield the value x to whoever called the method.

For example, consider the expression 2 + 3. If you were to say int x = 2 + 3, you would expect x to have value 5 afterwards. Now consider a method:

public static int plus(int a, int b){
    return a + b;
}

And the statement: int x = plus(2, 3);. Same here, we would expect x to have value 5 afterwards. The computation is done, and whoever is waiting on that value (of type int) receives and uses the value however a single value of that type would be used in place of it. For example:

int x = plus(plus(1,2),plus(3,plus(4,1)); -> x has value 11.

Back to your example: you need to assign a variable to the String value returned from histogram(randomNum);, as such:

Change histogram(randomNum) to String s = histogram(randomNum).

This will make it all compile, but you'll hit one final roadblock: The thing won't run! This is because a runnable main method needs to be static. So change your main method to have the signature:

public static void main(String[] args){...}

Then hit the green button!

2 of 3
2

For starters your main method should be static:

public static void main(String[] Args)

Instance methods can not be called without an instance of the class they belong to where static methods can be called without an instance. So if you want to call your other methods inside the main method they must also be static unless you create an object of type prog310t then use the object to call the methods example:

public static void main(String[] Args)
{
     prog310t test = new prog310t();
     test.histogram(1);
}

But in your case you probably want to do:

public static String histogram (int randomNum)

public static void main(String[] Args)
{
     histogram(1);
}

Also you are not catching the return of histogram() method in your main method you should do like this:

  System.out.print(histogram(randomNum) + "\n");

Or

String test = histogram(randomNum);
System.out.print(test + "\n");

Static methods are part of a class and can be called without an instance but instance methods can only be called from an instance example:

public class Test
{
    public static void main(String[] args)
    {
        getNothingStatic();// this is ok
        getNothing(); // THIS IS NOT OK IT WON'T WORK NEEDS AN INSTANCE
        Test test = new Test();
        test.getNothing(); // this is ok
        getString(); // this is ok but you are not capturing the return value
        String myString = getString(); // now the return string is stored in myString for later use
    }
    public void getNothing()
    {
    }
    public static void getNothingStatic()
    {
    }
    public static String getString()
    {
        return "hello";
    }
}

Void means the method is not returning anything it is just doing some processing. You can return primitive or Object types in place of void but in your method you must specify a return if you don't use void.

Discussions

What Every Java Developer should know about String
[String.split() is] particularly useful if you dealing with comma separated file (CSV) and wanted to have individual part in a String array. No. Fuck you to hell if you parse CSV this way. It will break, and it will all be your fault. CSV allows for commas to be inside the records as long as they're quoted (or escaped), so a line in a CSV file could look like this: "There is only one record on this line. Because it has commas inside quotes, it is one single, unbroken record" String.split() will parse that line wrong. More on reddit.com
🌐 r/java
43
49
January 7, 2014
Java String Methods - You Need To Know

First method of the article is charAt... Great start! Then you realise this is probably every method available order alphabetically... This could be reduce to a "5 methods to know" to be really interesting.

More on reddit.com
🌐 r/programming
1
0
May 21, 2015
ELI5:What is a "String" in Java/programming and why is it known as a "complex added type" and other variable are known as "primitive" ?
In simplest terms, a String is an collection (referred to in programming as an "array") of characters. If you wanted to have a String that said "Reddit", it would literally be an array of character values ['R', 'e', 'd', 'd', 'i', 't']. The most fundamental difference between strings and primitive types such as "int" are that primitives are single values. Strings are collections of values (specifically, a collection of characters.) Characters themselves are primitive types. In addition, primitive types are pieces of data. Strings are objects, which are instances of classes. As far as classes go...basically, the people who made Java built a lot of code so Strings could be easy to use by programmers. (This is also why "String" is capitalized: the name of the class has a capital S.) Sorry if this is too technical...but it should answer your questions. More on reddit.com
🌐 r/explainlikeimfive
5
1
November 21, 2015
New Methods on Java Strings With JDK 11

Looks like they're taking the awful PHP "real_escape_string" approach to API development of finding increasingly strained synonyms when something needs to be fixed by making the Unicode-aware version of String.trim() be named String.strip() instead of just adding an overload for trim() that takes an option parameter to specify the trimming type.

A hypothetical alternative of trim(StringTrimOptions options), used as in these examples:

// First two are equivalent, and use the backward-compatible
// pre-existing trimming logic
str.trim();
str.trim(StringTrimOptions.ASCII_WHITESPACE);

// This example uses new Unicode-compatible trimming logic.
str.trim(StringTrimOptions.UNICODE_WHITESPACE);

... would be a much better solution that wouldn't require every developer going forward, in perpetuity, to have to discover what the non-obvious difference between trim() and strip() is (whereas the hypothetical StringTrimOptions alternative is self-explanatory to a large extent); and is forward-compatible should the need ever arise in the future for yet another type of string trimming.

(And with the isBlank() method being added too, which could probably also benefit from being able to specify the type of whitespace; maybe instead of StringSplitOptions the argument should be WhitespaceType instead so an overload ofisBlank() could read correctly with it too.)

More on reddit.com
🌐 r/programming
16
30
December 10, 2014
🌐
Refreshjava
refreshjava.com › java › string-class-methods
Useful String Class Methods in Java with Example - RefreshJava
Java string class methods are used to perform operations on string variables or string literals like compare two strings, find or replace a character/word in a string, add two strings etc.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string
String Class (Java.Lang) | Microsoft Learn
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the java.lang.Character Character class.
🌐
Reddit
reddit.com › r/java › what every java developer should know about string
r/java on Reddit: What Every Java Developer should know about String
January 7, 2014 - Strings in Java always have the same character encoding: UTF-16. Period. End of discussion. No exceptions. Where character encodings do come into play, rather, is whenever bytes are converted into a String or vice versa. And yes, the String methods/constructors that take a byte[] and yield ...
🌐
W3Schools
w3schools.com › java › java_ref_string.asp
Java String Methods
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › lang › String.html
String (Java SE 11 & JDK 11 )
January 20, 2026 - Index values refer to char code units, so a supplementary character uses two positions in a String. The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).
Find elsewhere
🌐
Cogentuniversity
cogentuniversity.com › post › methods-of-strings-class-in-java
Methods of Strings Class in Java
October 30, 2023 - Methods in Java are functions that perform specific tasks. The String class, being an integral part of Java, boasts a plethora of methods tailored to handle strings efficiently. These methods enable developers to manipulate strings, extract information, and transform data according to their programming needs.
🌐
Programiz
programiz.com › java-programming › library › string
Java String Methods | Programiz
Java has a lot of String methods that allow us to work with strings. In this reference page, you will find all the string methods available in Java.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the ...
🌐
DataCamp
datacamp.com › doc › java › java-strings
Java Strings
Strings can be created in Java using string literals or by instantiating the String class. ... Java provides a robust set of methods to perform various operations on strings.
🌐
Quora
quora.com › What-are-the-string-functions-in-Java
What are the string functions in Java? - Quora
Answer (1 of 3): I do not understand the question. First of all Java does not have functions but methods. This means that standalone functions, like in C, C++ and other languages, simply do not exist and in Java a method is always bound to a class. Static methods are somewhat different, but they ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-methods
Java String Methods - GeeksforGeeks
October 11, 2025 - Java provides a rich set of String methods that help perform various operations like comparison, searching, modification of string. Let's Understand one by one. ... This method provides the total count of characters in the string.
🌐
Harness Developer Hub
developer.harness.io › platform › variables & expressions › use java string methods
Use Java string methods | Harness Developer Hub
You can use almost any Java string method to extract characters or manipulate expression strings. The correct format for using a Java method with an expression is to wrap both the source expression and the method in the expression delimiter (<+...>), resulting in nested delimiters.
🌐
iO Flood
ioflood.com › blog › java-string-methods
Java String Methods: From Beginner to Expert
February 26, 2024 - In this example, we’ve created a string str with the value ‘Hello’. We then use the length() method to get the length of the string, which is 5. The length is then printed to the console. This is just a basic way to use Java String methods, but there’s so much more to learn about manipulating and analyzing strings in Java.
🌐
Scaler
scaler.com › topics › java › string-in-java
String in Java (With Examples) - Scaler Topics
May 10, 2022 - Now we are going to see some more examples of string in Java. In the above-given examples, val1 is created using the string literal, val2 is created using the new keyword and val3 is created by concatenating/joining val1 and val2. The length() method of string in Java is used to get the length of the string.
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Codecademy
codecademy.com › learn › learn-java › modules › learn-java-string-methods › cheatsheet
Learn Java: String Methods Cheatsheet | Codecademy
This method returns a String representing the text of the combined strings. ... In Java, the equals() string method tests for equality between two Strings.
🌐
Baeldung
baeldung.com › home › java › java string › java string api – new methods
Java String API – New Methods | Baeldung
January 8, 2024 - In this tutorial, we’ll explore and use these commonly used APIs for String manipulation introduced in Java 11 and 12. The indent() method adjusts the indentation of each line of the string based on the argument passed to it.
🌐
ScholarHat
scholarhat.com › home
What is String in Java - Java String Methods & Type (Examples)
Strings are immutable, meaning once created, their values cannot be changed. Java provides powerful built-in methods for string manipulation, such as concatenation, comparison, substring extraction, and pattern matching.
Published   August 30, 2025