0

Hi😶 Does anyone know how to write a java program which has a static class and computs the square of numbers😣

I know that I can use ( import math) but don't know how in classes😔

14th Oct 2018, 2:19 PM
Fatme_z
Fatme_z - avatar
4 Answers
+ 3
You cannot make a top level class static in Java, the compiler will not allow it, but you can make a nested class static in Java. can't you just do like this: class S{ public static void main(){ int n = 5; int square = n*n; System.out.println("Square is : " + square); } }
14th Oct 2018, 8:53 PM
Tanay
Tanay - avatar
+ 2
// is this what you are looking for? public class Temp{ static class Square{ int findSquare(int a){ return a*a; } } public static void main(String[] args) { Square square = new Square(); System.out.println("square of 5 is " +square.findSquare(5)); } }
15th Oct 2018, 7:00 AM
Tanay
Tanay - avatar
+ 1
Kind of tnx for your helping
15th Oct 2018, 1:09 PM
Fatme_z
Fatme_z - avatar
0
Tnx, with this. Do you know also about how to reference a variable to a static class and then print the result!??
14th Oct 2018, 11:04 PM
Fatme_z
Fatme_z - avatar