Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you create your own class? (java methods)

+17 votes
asked Mar 13 by Mabotse Moraba (250 points)

3 Answers

0 votes
answered Mar 17 by YOKESH V (440 points)
selected Mar 17 by Mabotse Moraba
 
Best answer
class hello{

}
0 votes
answered Mar 17 by Achu .V (140 points)
import java.util.*;

class Main{

public static void main(String[] args){

Scanner word=new Scanner(System.in);
System.out.println("hello:");

}

}
0 votes
answered Apr 16 by Zach (380 points)
What do you want to do with it? Java classes usually have sub-fields and sub-methods to structure it, similar to how you can do

new Object().toString()

and it'll print the string representation of an object.
For example, if you wanted a video-game-like vector class, you'd do

public class Vec3 {
    public double x, y, z; // three doubles for the three coordinates
    public Vec3(double x, double y, double z) { // constructor so you can actually make the Vec3 instance
        this.x = x;
        this.y = y;
        this.z = z;
    }
    // other stuff goes here
}

So if you wanted to create a vector for a point at (3,4,5), you would do

Vec3 point = new Vec3(3, 4, 5);

You can also override methods from superclasses (all Java classes extend Object if not explicitly specified), which is most commonly used to override toString(). For example:

public class Vec3 {
    public double x, y, z; // three doubles for the three coordinates
    public Vec3(double x, double y, double z) { // constructor so you can actually make the Vec3 instance
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public String toString() {
        return "[" + x + "," + y + "," + z + "]";
}

and that'll produce a string like "[3,4,5]" (given the example above with (3,4,5)).

Hope this helps!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...