为什么没有关键字“ static”不能使用此方法?

时间:2019-02-09 06:03:46

标签: java exception-handling

为什么“ div”方法在没有静态的情况下不起作用?当仅删除static关键字时,它给我错误,即..“ \ Playground:13:错误:无法从静态上下文引用非静态方法div(int,int)         System.out.println(div(42,0));“

public class Program {

    static int div(int a, int b) throws ArithmeticException {
        if(b == 0) {
            throw new ArithmeticException("Division by Zero");
        } else {
            return a / b;
        }
    }

    public static void main(String[] args) {
        System.out.println(div(42, 2));
    }

}

1 个答案:

答案 0 :(得分:0)

删除var quotedPrintable = require('quoted-printable'); var utf8 = require('utf8'); var Imap = require('imap'), inspect = require('util').inspect; var imap = new Imap({ user: 'xxxxxx@gmail.com', password: 'xxxxxxx', host: 'imap.gmail.com', port: 993, tls: true }); function openMail(cb) { imap.openBox("[Gmail]/All Mail", true, cb); } imap.once('ready', function() { openMail(function(err, box) { if (err) throw err; for(i = 0; i < n; i++){ var f = imap.seq.fetch((box.messages.total-i) + ':*', { bodies: ['HEADER.FIELDS (FROM)','TEXT']}); f.on('message', function(msg, seqno) { ('Message #%d', seqno); var prefix = '(#' + seqno + ') '; msg.on('body', function(stream, info) { var buffer = '', count = 0; stream.on('data', function(chunk) { count += chunk.length; buffer += chunk.toString('utf8'); }); stream.once('end', function() { if (info.which !== 'TEXT') (prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer))); else (prefix + 'Body [%s] Finished', inspect(info.which)); var bufferPrint = JSON.stringify(quotedPrintable.decode(utf8.decode(buffer))); }); }); }); } msg.once('attributes', function(attrs) { console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8)); }); msg.once('end', function() { console.log(prefix + 'Finished'); }); f.once('error', function(err) { console.log('Fetch error: ' + err); }); f.once('end', function() { imap.end(); }); }); }); imap.once('error', function(err) { console.log(err); }); imap.once('end', function() {}); imap.connect(); 时,这会使static成为 instance 方法。所以你必须改变

div

通过 实例 调用System.out.println(div(42, 2)); 。例如,

div