hey guys idk why this code didn't work.. can u please help me | Sololearn: Learn to code for FREE!

+1

hey guys idk why this code didn't work.. can u please help me

public class Employee{ private int empNum; public void setEmpNum(int emp) { empNum = emp; } public int getEmpNum() { return empNum; } } import java.util.Scanner ; public class Company { public static void main (String [] args){ Scanner input = new Scanner (System.in); System.out.println("Enter emp number "); int emp = input.nextInt (); Employee.setEmpNum(emp); System.out.println("Emp num ="+ Employee.getEmpNum()); } }

7/24/2021 12:11:32 PM

Habo Osman

11 Answers

New Answer

+2

Shahid Iqbal thx

+1

Alaska I'll try that.. though it works every time with them first

+1

oh wait.. now i get it

+1

nope.. still the same.. it says something about static and nonstatic methods

+1

it says that non-static method setEmpNum(int) cannot be referenced to static context Employee.setEmpNum(emp) ;

+1

guys i knew what's wrong.. i must create an object of it first then use it to reach the methods.. thx java for being so easy šŸ‘

+1

Alaska i did declare them static.. then another problem showed up i don't remember what was it.. anyway thanks šŸ–¤

+1

public class Employee{ private int empNum; public void setEmpNum(int emp) { empNum = emp; } public int getEmpNum() { return empNum; } } import java.util.Scanner ; public class Company { public static void main (String [] args){ Scanner input = new Scanner (System.in); System.out.println("Enter emp number "); int emp = input.nextInt (); Employee e = new Employee(); e.setEmpNum(emp); System.out.println("Emp num ="+e.getEmpNum()); } }

0

Import statements should be the first thing, after that declare all the classes

0

Habo Osman Yes, non-static methods should only be accessed through objects. While you can still declare method as static and call it using class's name.

0

No worries āœŒļø