【问题标题】:Running class inside function在函数内运行类
【发布时间】:2022-12-02 20:03:06
【问题描述】:

I am a new learner to Java; I want to run a class method inside a function thats not within the class scope inside VSC. However, my compiler tells me I have to remove the last curly braces from the class and require to instantiate the function as a method inside the class.

How do I use these in a dependent way?

For example:

import java.lang.Integer;

public class sortArray{

    public static void reorder(int[] T, int pos, int min){
        int i = 1;
        //int[] temp = new int[T.length];
        for( i = 1; i < T.length; i++){
            pos = i;
            min = T[i];
            for(int j = i+1; j < T.length; i++){
                if(Integer.compare(T[j], min) < 0){
                    pos=j;
                    min=T[j];
                    T[pos] = T[i];
                    T[i] = min; } }}}}


public static void read(){
    // run the class inside here
}

I come from a python background so best I explain from that background and correct me on the difference.

For example:

class sortArray:
    ....

def read():
    array = sortArray()
    reorder = array.reorder([1, 2, 3], 1, 1)
    return reorder
  • In java : a method HAS to be in a class. period
  • Looks like you just need to properly match your backets. What does \"run the class\" mean? Java has methods that run, not classes. Although there are Runnable classes that have a method run.
  • @matt I come from a python background so I am basing it from python interpretation. Something like class sortArray: ... a = sortArray(). I have updated the post!
  • Right, you cannot override the \"call\" method of a class and expect to be able to use it like that. If you want to create an instance of sortArray use sortArray x = new sortArray(); since you\'re using static methods you don\'t need an instance.

标签: java


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2020-06-05
  • 2019-03-31
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多