I would like to call the Salarycal() method from Employee inner class employeeInfo to class Exc
public class Employee {
public class employeeInfo{
int id;
String name;
int Salary;
public employeeInfo(int id,String name,int Salary){
this.id=id;
this.name=name;
this.Salary=Salary;
System.out.println(id+name+Salary);
}
public int Salarycal(){
int totalSalary =0;
int b=getId();
........
}
}
import Employer.Employee.employeeInfo;
public class Exc {
public static void main(String[] args) {
//want to access the salarycal() method in this class
}
}
this is how I would usually invoke a method of inner class
Inner inner = new Outer().new Inner();
inner.methodToInvoke();
Like #Ashish said:
public static void main(String[] args) {
new Employee().new employeeInfo().Salarycal();
}
PS. Read about Code Conventions for the Java TM Programming Language
Related
I started to programm in Java since Yesterday, and I have the biggest question of my entire programmer life(since Yesterday).
For example, let's say I have a code like this:
public class itsAClass {
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
return A;
}
}
I wanted to use the method something() in another Class to get the String Sentence of A, but I got only null.
How can I change the value of A, so that the another Class can get the Value "This should be changed"?
If you just want to bring this code to work you just can make something() static as well.
But this will be not the right way to approach this problem.
If you want to hold code in the main class you could do something like this:
public class AClass {
private String a;
public static void main() {
AClass myC = new AClass();
myC.setA("This should be changed");
// than use myC for your further access
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
}
If you want to access it by a external class without direct reference you can checkout the singleton pattern.
public class AClass {
private final static AClass INSTANCE = new AClass();
private String a;
public static void main() {
getSingleton().setA("This should be changed");
}
public String something() {
return a;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public static AClass getSingleton() {
return INSTANCE;
}
}
This way you can access it via AClass.getSingleton() from any location of your code.
You have to call your main() function.
In another class:
itsAClass aClassObj = new itsAClass();
aClassObj.main();
// or rather itsAClass.main() as it is a static function
// now A's value changed
System.out.println(aClassObj.something());
the way to set the value of private variable is by setter and getter methods in class.
example below
public class Test {
private String name;
private String idNum;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setAge( int newAge) {
age = newAge;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}
you can call method main() in method something().
public class itsAClass{
static private String A;
public static void main() {
A = "This should be changed";
}
public String something() {
main();
return A;
}
public static void main(String[] args){
itsAClass a1 = new itsAClass();
System.out.println(a1.something());// prints This should be changed
}
}
i want to access a variable inside another class
public class ephem_t{
public static void robel(){
int vflg;
Calendar t;
int iodc;
}
}
and i want to use the variables inside another class
public class testRobel{
public static void readfile(){
????????
}
}
i want to do like
public class testRobel{
public static void readfile(){
ephem_t eph = new ephem_t();
eph.robel.vflg = 1;
}
}
You should declare your class variables outside of a method, just
public class ephem_t {
int vflg;
Calendar t;
int iodc;
}
And an option is to use setters & getters to access those variables.
Or jus create a new object of your class.
public class testEphm_t {
public static void readfile(){
ephem_t eph = new ephem_t();
eph.vflg = 1;
}
}
What is wrong with my code? Compiler says non static variable cannot be referenced from a static context
package nestedclass;
public class Nestedclass {
class Student {
String name;
int age;
long roll;
public Student(String name, int age, long roll) {
this.name = name;
this.age = age;
this.roll = roll;
}
public void show() {
System.out.println("name : "+name+" age : "+age+" roll : "+roll);
}
}
public static void main(String[] args) {
//Nestedclass=new Nestedclass();
Student ob=new Student("ishtiaque",10,107060);
ob.show();
// TODO code application logic here
}
}
The nested Student class isn't static, that's why the compiler complains. There are a couple of ways to get out of this situation: make Student static, or instantiate Nestedclass and provide a nonstatic method in Nestedclass that does the actual work on an instance of Student, and that you call from main:
private void run() {
Student ob = new Student("ishtiaque", 10, 107060);
ob.show();
}
public static void main(String[] args) {
new Nestedclass().run();
}
or, if you like oneliners you could also do
public static void main(String[] args) {
new Nestedclass().new Student("ishtiaque", 10, 107060).show();
}
Personally I prefer the second method (with the helper method) as it's easier to refactor afterwards.
Your inner class is not declared as static; therefore, you cannot access it from the static method main. Change your inner class declaration to static class Student
You need to initiate the static class then Use the new of that class in order to initiate the their inner none classes. Or you can declare your Student inner class as static class.
first solution
Student ob = new Nestedclass().new Student("ishtiaque", 10, 107060);
second solution
static class Strung {...}
Hope that helps.
this will work
public class Student
{
String name;
int age;
long roll;
public Student(String name, int age, long roll) {
this.name = name;
this.age = age;
this.roll = roll;
}
public void show()
{
System.out.println("name : "+name+" age : "+age+" roll : "+roll);
}
public static void main(String[] args) {
Student ob=new Student("ishtiaque",10,107060);
ob.show();
}
}
I am a complete beginner in java, so please forgive me if this question is not up to the standard of this website:
class person{
String name;
int age;
}
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
t1.age=56;
}
Here I am trying to access the variable age and name of class to assign them values person, which happens to be the super class of person. But the compiler is giving error. I even tried to make the name variable and age variable as public. But the compiler is still reporting an error. I want to know the reason why I can't access superclass variable in subclass directly and assign values to them.
You are not allowed to write arbitrary code directly within a class body. The closest thing to what you have written is
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
{
t1.age=56;
}
}
This is called the instance initializer block.
In general, it is not a good idea to access variables directly. Consider using something like the code below.
Test driver
package com.example.input;
public class TestPeople {
public static void main(String[] args) {
Person s1 = new Person();
s1.setName("student 1").setAge(19);
Teacher t1 = new Teacher("Dr. Fun",0);
t1.setAge(56);
System.out.println("Student " + s1.getName() + ", " + s1.getAge());
System.out.println("Teacher " + t1.getName() + ", " + t1.getAge());
}
}
Class Person
package com.example.input;
public class Person {
private String name;
private int age;
public Person() {};
public Person(String aName, int anAge) {
setName(aName).setAge(anAge);
}
public String getName() {return name;}
public int getAge() {return age;}
public Person setName(String aName) { name = aName; return this;}
public Person setAge(int anAge) { age = anAge; return this;}
}
Class Teacher
package com.example.input;
public class Teacher extends Person {
public Teacher() {
super();
}
public Teacher(String aName, int anAge) {
super(aName, anAge);
}
}
Actually you are making an assignment of variable of external class in existing class body which is not legal.Make the assignment in a method or constructor or in a anonymous block.
class person{
String name;
int age;
}
class teacher extends person{
person s1=new person();
teacher t1=new teacher();
// t1.age=56; Error here
public teacher()
{
t1.age=56;
}
//or method
public void setAge()
{
t1.age=56;
}
}
do whatever suits your program and you.
In my main class, I have a static method which I pass the array into. It is a static method because if I want to pass something from the main class body to this method, it must be static. In a separate class I have a series of getters and setters (which must be non static ).
How can I pass my static array in and use the non-static getters and setters?
EDIT- In the arraySearch method...I cannot pass in the Person Array and access the getters in the Person Class
public class Main {
public static void main(String[] args) {
Person One = new Person("Alice","Foo", 22, false);
Person Two = new Person("Alice", "Foo",22, false);
Person Three = new Person("Bob","Bar",99, false);
Person Four = new Person("Joe","Blogs",64, false);
Person Five = new Person("Jane", "Joe",42, false);
Person [] People = {One,Two,Three,Four,Five};
printArray(People);
}
public static void printArray(Person [] People)
{
for(int i=0;i<People.length;i++)
{
System.out.println(People[i]);
}
}
public void arraySearch(Person [] People)
{
for(int i=0;i<People.length;i++) //Searches the Array of Objects
{
String firstName = Person.getFirstName();
String secondName=Person.getSecondName();
if((firstName.equals("Joe")&&secondName.equals("B" + //Searches for Joe Blogs and Jane Joe
"logs"))|| ((firstName.equals("Ja" +
"ne")&&secondName.equals("Joe"))))
{
int age=Person.getAge();
Person.setAge(age+1); //Increments Age by 1
}
}
}
}
public class Person {
private String mfirstName;
private String msecondName;
private int mage;
private boolean misRetired;
public Person(String firstName,String secondName,int age, boolean isRetired)
{
mfirstName=firstName;
msecondName=secondName;
mage=age;
misRetired=isRetired;
}
//GETTERS
public String getFirstName()
{
return mfirstName;
}
public String getSecondName()
{
return msecondName;
}
public int getAge()
{
return mage;
}
public boolean getRetired()
{
return misRetired;
}
//SETTERS
public void setFirstName(String firstName)
{
mfirstName=firstName;
}
public void setSecondName(String secondName)
{
msecondName=secondName;
}
public void setAge(int age)
{
mage=age;
}
public void setRetired(boolean isRetired)
{
misRetired=isRetired;
}
//STRING
public String toString()
{
return (mfirstName+"-"+msecondName+"-"+mage+"-"+misRetired);
}
}
This is very basic Java question. You need to create instance of object containing setter/getters from your static method. You can also pass static array in setter of this object. Then you should be able to call those getter/setter methods.
public class Main
{
public static void main(String[] args)
{
MyClass myclass = new MyClass();
myclass.setArgs(args);
System.out.println(myclass.getArgs());
}
}
public class MyClass
{
private String[] args;
public String[] getArgs()
{
return args;
}
public void setArgs(String[] args)
{
this.args= args;
}
}
You have to create an object instance from the class with the getters.
The Amit answer is correct; this just has some more info and more closely matches the situation you describe in your question.
Your basic premise "It is a static method because if I want to pass something from the main class body to this method, it must be static." is wrong. The method to which you pass the array does not need to be static. Here is some code:
public final class Main
{
private static final String[] staticOTron =
{
"one",
"two",
"three"
};
public static void main(final String[] args)
{
String[] hootBerrySause;
Tool tool = new Tool();
tool.setStaticOTron(staticOTron);
hootBerrySause = tool.getStaticOTron();
for (String value : hootBerrySause)
{
System.out.println("Value: " + value);
}
}
}
// this can be in a different file.
public final class Tool
{
private static String[] staticOTron;
public void setStaticOTron(final String[] newValue)
{
staticOTron = newValue;
}
public String[] getStaticOTron()
{
return staticOTron;
}
}
Sunil kumar from vmoksha
Your asking deeper navigation
Just create the instance of particular or create the getter &and setter in the main
class